tubifier / frames.py
wldmr's picture
app file
837fdb6
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<srt.txt
def addCaptionToImage(caption):
proc_list = []
for current_frame, current_caption in enumerate(caption.split('\n'), start=1):
print(f"{current_frame:04d}", current_caption)
#current_frame=182
#current_caption='with this method as compared to just'
cmd = 'convert frame_'+f"{current_frame:04d}"+'.jpg -undercolor Black -fill white -gravity South -pointsize 25 -annotate +0+10'
cmd_call = cmd.split()
# the 'split' command would also split the input caption
# therefore it has to be added to the array after the split
cmd_call.append(current_caption)
cmd_call.append('frame_'+f"{current_frame:04d}"+'.jpg')
#cmd_call
working_dir = './workdir'
with sp.Popen(cmd_call,cwd=working_dir, stderr=sp.PIPE) as proc:
proc_list.append(proc.wait())
return proc_list
def removeFilesInWorkdir():
result =''
working_dir = './workdir'
try:
for f in os.listdir(working_dir):
os.remove(os.path.join(working_dir, f))
except:
result = 'Error: Not all files could be removed.'
return result
def renameOutputVideo(filenme):
result = ''
working_dir = './workdir'
shelf_dir = './shelf'
input_filename = working_dir+'/'+'output_video.mp4'
output_filename = shelf_dir+'/'+filenme+'.mp4'
try:
os.rename(input_filename,output_filename)
except:
result = 'Error: Could not rename file.'
return result