From cb556f53d5b22b9c37de6881304934b909dca103 Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 8 Dec 2015 22:49:02 +0100 Subject: [PATCH] Batch processing work --- export-video.py | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/export-video.py b/export-video.py index c2fb27b..8f3c042 100755 --- a/export-video.py +++ b/export-video.py @@ -2,7 +2,9 @@ # 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 getopt import re @@ -35,7 +37,8 @@ params = { "webm_acommand": "-strict -2", "verbose": True, "pattern": "", - "twopass": False} + "twopass": False, + "recursive": False} VERSION = "0.1dev" exec_ffmpeg = os.path.join("ffmpeg") @@ -45,9 +48,8 @@ def log(msg): sys.stdout.write(msg + "\n") -def listFile(directory, pattern): - if params["verbose"]: - print("searching in directory", directory) +def listFile(directory, pattern, recursive): + log("searching in directory" + directory) files_list = [] try: dirs = os.listdir(directory) @@ -56,10 +58,11 @@ def listFile(directory, pattern): return 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)) else: if re.search(pattern, i) is not None: + log("file found : " + i) files_list.append(directory + "/" + i) return files_list @@ -106,7 +109,10 @@ def processArg(sysarg): params["vbitrate"] = arg if opt in ("-2", "--twopass"): 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): @@ -134,8 +140,8 @@ def encode_h264(src, dest): ] + params["h264_acommand"] + [ dest+".mp4" ] - subprocess.call(firstpass) - subprocess.call(secondpass) + subprocess.run(firstpass) + subprocess.run(secondpass) else: log("one pass encoding started ...") encode = [ @@ -146,24 +152,34 @@ def encode_h264(src, dest): ] + params["h264_vcommand"] + [ "-b:a", params["abitrate"] ] + params["h264_acommand"] + [ - dest+".mp4" + dest+".mp4", + "-loglevel", "quiet" ] - subprocess.call(encode) + subprocess.run(encode) if __name__ == "__main__": log("export_video v" + VERSION + "FFMPEG exec " + exec_ffmpeg) processArg(sys.argv[1:]) 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) if os.path.isdir(params["file_input"]): - if params["verbose"]: - log(params["file_input"]+" is a directory") - video_files = listFile(params["file_input"], params["pattern"]) + 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") + 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: if os.path.isfile(params["file_input"]): - if (params["file_output"]): + if not params["file_output"]: params["file_output"] = getFileName(params["file_input"]) else: if(os.path.isdir(params["file_output"])):