From 862b0783d731dd8980698fb035b78300703b9076 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Tue, 10 Mar 2020 23:43:12 +0530 Subject: Added video generator --- src/VHSImage.py | 52 +++++++++++++++++++++------------- src/VHSVideo.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 src/VHSVideo.py (limited to 'src') diff --git a/src/VHSImage.py b/src/VHSImage.py index 51688b1..81f446d 100644 --- a/src/VHSImage.py +++ b/src/VHSImage.py @@ -146,25 +146,39 @@ if __name__ == "__main__": build_background("bkg.jpg", 25) """ -def generateVHSStyle(infile, outfile): - cut_rows = bool(random.getrandbits(1)) - offset = random.choice([0,5,10,15,20,25]) - logger.info("Saturating the image") - offset_hue(infile,"saturated.jpg") - if cut_rows: - logger.info("Shifting the image") - mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, True, "shifted.jpg") +def generateVHSStyle(infile, outfile, silence=False): + if silence: + cut_rows = bool(random.getrandbits(1)) + offset = random.choice([0,5,10,15,20,25]) + offset_hue(infile,"saturated.jpg") + if cut_rows: + mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, True, "shifted.jpg") + else: + mod_image_repeat_rows("saturated.jpg", 0, 0, 0, True, "shifted.jpg") + add_date("shifted.jpg","noisy.jpg") + add_date("noisy.jpg",outfile, bottom_offset=offset) + os.remove("shifted.jpg") + os.remove("saturated.jpg") + os.remove("noisy.jpg") else: - logger.info("Not applying lines effect") - mod_image_repeat_rows("saturated.jpg", 0, 0, 0, True, "shifted.jpg") - logger.info("Adding noise") - add_date("shifted.jpg","noisy.jpg") - logger.info("Adding text") - add_date("noisy.jpg",outfile, bottom_offset=offset) - logger.info("Generated Image: out.jpg") - logger.info("Removing residual files") - os.remove("shifted.jpg") - os.remove("saturated.jpg") - os.remove("noisy.jpg") + cut_rows = bool(random.getrandbits(1)) + offset = random.choice([0,5,10,15,20,25]) + logger.info("Saturating the image") + offset_hue(infile,"saturated.jpg") + if cut_rows: + logger.info("Shifting the image") + mod_image_repeat_rows("saturated.jpg", 0.012, 50, 10, True, "shifted.jpg") + else: + logger.info("Not applying lines effect") + mod_image_repeat_rows("saturated.jpg", 0, 0, 0, True, "shifted.jpg") + logger.info("Adding noise") + add_date("shifted.jpg","noisy.jpg") + logger.info("Adding text") + add_date("noisy.jpg",outfile, bottom_offset=offset) + logger.info("Generated Image: out.jpg") + logger.info("Removing residual files") + os.remove("shifted.jpg") + os.remove("saturated.jpg") + os.remove("noisy.jpg") #generateVHSStyle("s.jpg","o.jpg") \ No newline at end of file diff --git a/src/VHSVideo.py b/src/VHSVideo.py new file mode 100644 index 0000000..e644074 --- /dev/null +++ b/src/VHSVideo.py @@ -0,0 +1,87 @@ +import os +import cv2 +from src.VHSImage import generateVHSStyle +from os.path import isfile, join +import numpy as np +import subprocess +import logzero +from logzero import logger +from logzero import setup_logger + +def SaveVid(path): + vidObj = cv2.VideoCapture(path) + count = 0 + success = 1 + while success: + success, image = vidObj.read() + cv2.imwrite("frames/"+str(count)+".jpg", image) + #os.rename("frames/"+str(count)+".jpg", os.path.splitext("frames/"+str(count)+".jpg")[0]) + count += 1 + +def Style(pathToFrames): + files = [f for f in os.listdir(pathToFrames) if isfile(join(pathToFrames, f))] + count = 0 + for i in files: + count += 1 + f = str(i) + fi = pathToFrames + f + out = fi + ".jpg" + + generateVHSStyle(fi, out, silence=True) + os.rename(out, fi) + print("--------") + print("On Frame: ") + print(count) + print("Out of") + print(len(files)) + print("--------") + cwd = os.getcwd() + os.chdir(pathToFrames) + c = "find ./ -name \"*.jpg\" -exec sh -c 'mv $0 `basename \"$0\" .jpg`' '{}' \; ;" + os.system(c) + os.chdir(cwd) + +def generateVideo(outfile, path, infile): + frame_array = [] + files = [int(f) for f in os.listdir(path) if isfile(join(path, f))] + files.sort() + + duration = subprocess.check_output(['ffprobe', '-i', infile, '-show_entries', 'format=duration', '-v', 'quiet', '-of', 'csv=%s' % ("p=0")]) + fps = len(files)/float(duration) + print("FPS", fps) + + for i in range(len(files)): + filename=path + str(files[i]) + img = cv2.imread(filename) + height, width, layers = img.shape + size = (width,height) + frame_array.append(img) + out = cv2.VideoWriter(outfile,cv2.VideoWriter_fourcc(*'MP4V'), fps, size) + for i in range(len(frame_array)): + out.write(frame_array[i]) + out.release() + + +def VHS_Vid(infile, outfile): + path = './frames/' + os.system("rm frames/*") + os.system("mkdir frames") + logger.info("Exctracting Frames") + try: + SaveVid(infile) + except: + logger.debug("Extracted Frames") + logger.info("Applying A E S T H E T I C S") + Style(path) + logger.info("Generating Vidio") + generateVideo("temp.mp4", path, infile) + logger.info("Extracting audio of original video") + os.system("ffmpeg -i %(infile)s -vn -acodec copy output-audio.aac") + logger.info("Merging audio") + os.system("ffmpeg -i temp.mp4 -i output-audio.aac -c copy %(outfile)s") + logger.info("Removing residual files") + os.remove("temp.mp4") + os.remove("output-audio.aac") + + +#VHS_Vid("video.mp4","video2.mp4") \ No newline at end of file -- cgit v1.2.3