176 lines
4.9 KiB
Python
Executable file
176 lines
4.9 KiB
Python
Executable file
#!/usr/bin/env python
|
|
|
|
# export-video.py
|
|
# ---------------
|
|
# ré-écriture complète du script bash eponyme en python
|
|
import sys
|
|
import getopt
|
|
import re
|
|
import os
|
|
import subprocess
|
|
|
|
|
|
def usage():
|
|
print("export-video -i input -o output [-d --desinterlace]")
|
|
|
|
params = {
|
|
"desinterlace": False,
|
|
"file_output": "video",
|
|
"file_input": "",
|
|
"vbitrate": "600k",
|
|
"abitrate": "96k",
|
|
"video_quality": "good",
|
|
"h264_vcommand": [
|
|
"-trellis", "0",
|
|
"-me_method", "umh",
|
|
"-subq", "8",
|
|
"-me_range", "16",
|
|
"-bf", "3",
|
|
"-rc_lookahead", "16",
|
|
"-g", "250"
|
|
],
|
|
"h264_acommand": ["-strict", "experimental"],
|
|
"webm_vcommand": "-rc_lookahead 16 -keyint_min 0 -g 250 -skip_threshold \
|
|
0 -level 116",
|
|
"webm_acommand": "-strict -2",
|
|
"verbose": True,
|
|
"pattern": "",
|
|
"twopass": False}
|
|
|
|
VERSION = "0.1dev"
|
|
exec_ffmpeg = os.path.join("ffmpeg")
|
|
|
|
|
|
def log(msg):
|
|
sys.stdout.write(msg + "\n")
|
|
|
|
|
|
def listFile(directory, pattern):
|
|
if params["verbose"]:
|
|
print("searching in directory", directory)
|
|
files_list = []
|
|
try:
|
|
dirs = os.listdir(directory)
|
|
except:
|
|
print("error : not a directory?")
|
|
return
|
|
|
|
for i in dirs:
|
|
if os.path.isdir(directory + "/" + i):
|
|
files_list.extend(listFile(directory + "/" + i, pattern))
|
|
else:
|
|
if re.search(pattern, i) is not None:
|
|
files_list.append(directory + "/" + i)
|
|
return files_list
|
|
|
|
|
|
def getFileName(path):
|
|
return os.path.splitext(os.path.basename(path))[0]
|
|
|
|
|
|
def processArg(sysarg):
|
|
try:
|
|
opts, args = getopt.getopt(
|
|
sysarg, "hi:o:dp:a:v:2",
|
|
[
|
|
"help",
|
|
"input=",
|
|
"output=",
|
|
"desinterlace",
|
|
"pattern=",
|
|
"abitrate=",
|
|
"vbitrate=",
|
|
"twopass"
|
|
]
|
|
)
|
|
except getopt.GetoptError:
|
|
usage()
|
|
sys.exit(2)
|
|
for opt, arg in opts:
|
|
if opt in ("-h, --help"):
|
|
usage()
|
|
sys.exit()
|
|
if opt in ("-o", "--output"):
|
|
log("output spécifié " + arg)
|
|
params["file_output"] = arg
|
|
if opt in ("-d", "--desinterlace"):
|
|
log("desinterlace on")
|
|
params["desinterlace"] = True
|
|
if opt in ("-i", "--input"):
|
|
params["file_input"] = arg
|
|
if opt in ("-p", "--pattern"):
|
|
params["pattern"] = "^\\w*."+arg+"$"
|
|
if opt in ("-a", "--abitrate"):
|
|
params["abitrate"] = arg
|
|
if opt in ("-v", "--vbitrate"):
|
|
params["vbitrate"] = arg
|
|
if opt in ("-2", "--twopass"):
|
|
log("TwoPass encoding on")
|
|
params["twopass"] == True
|
|
|
|
|
|
def encode_h264(src, dest):
|
|
if (params["twopass"]):
|
|
log("two pass encoding started ...")
|
|
firstpass = [
|
|
exec_ffmpeg,
|
|
"-y",
|
|
"-i", src,
|
|
"-codec:va", "libx264",
|
|
"-b:v", params["vbitrate"],
|
|
"-pass", "1",
|
|
] + params["h264_vcommand"] + [
|
|
"-f", "rawvideo",
|
|
"/dev/null"
|
|
]
|
|
secondpass = [
|
|
exec_ffmpeg,
|
|
"-i", src,
|
|
"-pass", "2",
|
|
"-codec:v", "libx264",
|
|
"-b:v ", params["vbitrate"]
|
|
] + params["h264_vcommand"] + [
|
|
"-b:a", params["abitrate"]
|
|
] + params["h264_acommand"] + [
|
|
dest+".mp4"
|
|
]
|
|
subprocess.call(firstpass)
|
|
subprocess.call(secondpass)
|
|
else:
|
|
log("one pass encoding started ...")
|
|
encode = [
|
|
exec_ffmpeg,
|
|
"-i", src,
|
|
"-codec:v", "libx264",
|
|
"-b:v ", params["vbitrate"]
|
|
] + params["h264_vcommand"] + [
|
|
"-b:a", params["abitrate"]
|
|
] + params["h264_acommand"] + [
|
|
dest+".mp4"
|
|
]
|
|
subprocess.call(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"])
|
|
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"])
|
|
else:
|
|
if os.path.isfile(params["file_input"]):
|
|
if (params["file_output"]):
|
|
params["file_output"] = getFileName(params["file_input"])
|
|
else:
|
|
if(os.path.isdir(params["file_output"])):
|
|
params["file_input"] += getFileName(params["file_input"])
|
|
log(params["file_input"] + " is a file")
|
|
encode_h264(params["file_input"], params["file_output"])
|
|
else:
|
|
print("input file desn't exist, bye bye")
|
|
sys.exit(2)
|
|
sys.exit(0)
|