aboutsummaryrefslogtreecommitdiff
path: root/src/VHSVideo.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/VHSVideo.py')
-rw-r--r--src/VHSVideo.py87
1 files changed, 87 insertions, 0 deletions
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