from ast import Try import subprocess as sp import os # show current venv: echo $VIRTUAL_ENV # import sys # del sys.modules['frames'] # transcript module # 1. extract timestamps from transcript # 2. extract captions from transcript # this module # 3. extract frames at timestamps # 4. add caption to each frame # 5. convert images to mp4 video # converts a list of images to a mp4 video def convertImageToVideo(): cmd = "ffmpeg -y -f image2 -i frame_%04d.jpg output_video.mp4" cmd_call = cmd.split() working_dir = './workdir' with sp.Popen(cmd_call,cwd=working_dir, stderr=sp.PIPE) as proc: result = proc.stderr.read() return [proc.wait(),result] # extract a frame as jpg image file # from a video at a given timestamp # num=0; for p in $(cat timestamps); do ((num++)); printf "$num $p\r"; dnum=$(printf "%03d" "$num"); ffmpeg -ss $p -i "$mp4file" -frames:v 1 out_$dnum.jpg >& ffmpeg.out; done def extractImagesFromVideo(timestamps): working_dir = './workdir' input_file = 'input_video.mp4' if not os.path.isfile(working_dir+'/'+input_file): return 'Error: File '+input_file+' is missing, create the file first.' # create a working directory for the files if not os.path.isdir(working_dir): print('There is no working directory. Create a new one.') os.mkdir(working_dir) proc_list = [] for current_frame, current_timestamp in enumerate(timestamps, start=1): print(f"{current_frame:04d}", current_timestamp) cmd = 'ffmpeg -y -ss '+str(current_timestamp)+' -i '+input_file+' -frames:v 1 frame_'+f"{current_frame:04d}"+'.jpg' cmd_call = cmd.split() with sp.Popen(cmd_call,cwd=working_dir, stderr=sp.PIPE) as proc: proc_list.append(proc.wait()) return proc_list # add caption to each image # 'convert' porgram is from the 'imagemagick' package # num=0; while read p; do ((num++)); dnum=$(printf "%03d" "$num"); printf "$dnum $p\r"; convert out_$dnum.jpg -undercolor Black -fill white -gravity South -pointsize 25 -annotate +0+10 "$p" out_$dnum.jpg >& ffmpeg.out; done