aboutsummaryrefslogtreecommitdiff
path: root/src/VHSVideo.py
blob: e6440744a7173723d5d559bba9d9238e542be429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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")