|
from ast import Try |
|
import subprocess as sp |
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
|
|
|
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.' |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
def addCaptionToImage(caption): |
|
proc_list = [] |
|
for current_frame, current_caption in enumerate(caption.split('\n'), start=1): |
|
print(f"{current_frame:04d}", current_caption) |
|
|
|
|
|
|
|
cmd = 'convert frame_'+f"{current_frame:04d}"+'.jpg -undercolor Black -fill white -gravity South -pointsize 25 -annotate +0+10' |
|
cmd_call = cmd.split() |
|
|
|
|
|
cmd_call.append(current_caption) |
|
cmd_call.append('frame_'+f"{current_frame:04d}"+'.jpg') |
|
|
|
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 |