Batch processing work

This commit is contained in:
Yorick Barbanneau 2015-12-08 22:49:02 +01:00
parent 6c7ba12990
commit cb556f53d5

View file

@ -2,7 +2,9 @@
# export-video.py # export-video.py
# --------------- # ---------------
# ré-écriture complète du script bash eponyme en python # little python script to encode vide file for the web
#
import sys import sys
import getopt import getopt
import re import re
@ -35,7 +37,8 @@ params = {
"webm_acommand": "-strict -2", "webm_acommand": "-strict -2",
"verbose": True, "verbose": True,
"pattern": "", "pattern": "",
"twopass": False} "twopass": False,
"recursive": False}
VERSION = "0.1dev" VERSION = "0.1dev"
exec_ffmpeg = os.path.join("ffmpeg") exec_ffmpeg = os.path.join("ffmpeg")
@ -45,9 +48,8 @@ def log(msg):
sys.stdout.write(msg + "\n") sys.stdout.write(msg + "\n")
def listFile(directory, pattern): def listFile(directory, pattern, recursive):
if params["verbose"]: log("searching in directory" + directory)
print("searching in directory", directory)
files_list = [] files_list = []
try: try:
dirs = os.listdir(directory) dirs = os.listdir(directory)
@ -56,10 +58,11 @@ def listFile(directory, pattern):
return return
for i in dirs: for i in dirs:
if os.path.isdir(directory + "/" + i): if os.path.isdir(directory + "/" + i) and recursive:
files_list.extend(listFile(directory + "/" + i, pattern)) files_list.extend(listFile(directory + "/" + i, pattern))
else: else:
if re.search(pattern, i) is not None: if re.search(pattern, i) is not None:
log("file found : " + i)
files_list.append(directory + "/" + i) files_list.append(directory + "/" + i)
return files_list return files_list
@ -106,7 +109,10 @@ def processArg(sysarg):
params["vbitrate"] = arg params["vbitrate"] = arg
if opt in ("-2", "--twopass"): if opt in ("-2", "--twopass"):
log("TwoPass encoding on") log("TwoPass encoding on")
params["twopass"] == True params["twopass"] = True
if opt in ("-r", "--recursive"):
log("Recursive search of video files")
params["recursive"] = True
def encode_h264(src, dest): def encode_h264(src, dest):
@ -134,8 +140,8 @@ def encode_h264(src, dest):
] + params["h264_acommand"] + [ ] + params["h264_acommand"] + [
dest+".mp4" dest+".mp4"
] ]
subprocess.call(firstpass) subprocess.run(firstpass)
subprocess.call(secondpass) subprocess.run(secondpass)
else: else:
log("one pass encoding started ...") log("one pass encoding started ...")
encode = [ encode = [
@ -146,24 +152,34 @@ def encode_h264(src, dest):
] + params["h264_vcommand"] + [ ] + params["h264_vcommand"] + [
"-b:a", params["abitrate"] "-b:a", params["abitrate"]
] + params["h264_acommand"] + [ ] + params["h264_acommand"] + [
dest+".mp4" dest+".mp4",
"-loglevel", "quiet"
] ]
subprocess.call(encode) subprocess.run(encode)
if __name__ == "__main__": if __name__ == "__main__":
log("export_video v" + VERSION + "FFMPEG exec " + exec_ffmpeg) log("export_video v" + VERSION + "FFMPEG exec " + exec_ffmpeg)
processArg(sys.argv[1:]) processArg(sys.argv[1:])
if not params["file_input"]: if not params["file_input"]:
print("you must specify a file / directory" + params["file_input"]) log("you must specify a file / directory" + params["file_input"])
sys.exit(2) sys.exit(2)
if os.path.isdir(params["file_input"]): if os.path.isdir(params["file_input"]):
if params["verbose"]: if os.path.isfile(params["file_output"]):
log("Can't batch export to a file! Bye Bye")
sys.exit(2)
log(params["file_input"] + " is a directory") log(params["file_input"] + " is a directory")
video_files = listFile(params["file_input"], params["pattern"]) video_files = listFile(
params["file_input"],
params["pattern"],
params["recursive"]
)
for f in video_files:
log("batch encode | file : " + f)
encode_h264(f, params["file_output"] + getFileName(f))
else: else:
if os.path.isfile(params["file_input"]): if os.path.isfile(params["file_input"]):
if (params["file_output"]): if not params["file_output"]:
params["file_output"] = getFileName(params["file_input"]) params["file_output"] = getFileName(params["file_input"])
else: else:
if(os.path.isdir(params["file_output"])): if(os.path.isdir(params["file_output"])):