Using logging for log activity

This commit is contained in:
Yorick Barbanneau 2015-12-08 23:21:09 +01:00
parent b98d0d2c73
commit 02cda94439

View file

@ -10,6 +10,9 @@ import getopt
import re import re
import os import os
import subprocess import subprocess
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
def usage(): def usage():
@ -43,10 +46,6 @@ params = {
VERSION = "0.1dev" VERSION = "0.1dev"
def log(msg):
sys.stdout.write(msg + "\n")
def execute(command, shell=False): def execute(command, shell=False):
""" """
Execute a system command and return its results. Execute a system command and return its results.
@ -58,7 +57,7 @@ def execute(command, shell=False):
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate() stderr=subprocess.PIPE).communicate()
except OSError as ex: except OSError as ex:
print('Could not run command %s: %s', command, ex) logging.error('Could not run command %s: %s', command, ex)
return return
return stdout.strip() return stdout.strip()
@ -70,20 +69,21 @@ def find_executable(exec_name):
def listFile(directory, pattern, recursive): def listFile(directory, pattern, recursive):
log("searching in directory" + directory) logging.info('searching in directory %s', directory)
files_list = [] files_list = []
try: try:
dirs = os.listdir(directory) dirs = os.listdir(directory)
except: except:
print("error : not a directory?") logging.error('error : %s not a directory?', directory)
return return
for i in dirs: for i in dirs:
if os.path.isdir(directory + "/" + i) and recursive: if os.path.isdir(directory + "/" + i) and recursive:
logging.info('directory found : %s', i)
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) logging.info('file found : %s', i)
files_list.append(directory + "/" + i) files_list.append(directory + "/" + i)
return files_list return files_list
@ -115,10 +115,8 @@ def processArg(sysarg):
usage() usage()
sys.exit() sys.exit()
if opt in ("-o", "--output"): if opt in ("-o", "--output"):
log("output spécifié " + arg)
params["file_output"] = arg params["file_output"] = arg
if opt in ("-d", "--desinterlace"): if opt in ("-d", "--desinterlace"):
log("desinterlace on")
params["desinterlace"] = True params["desinterlace"] = True
if opt in ("-i", "--input"): if opt in ("-i", "--input"):
params["file_input"] = arg params["file_input"] = arg
@ -129,16 +127,14 @@ def processArg(sysarg):
if opt in ("-v", "--vbitrate"): if opt in ("-v", "--vbitrate"):
params["vbitrate"] = arg params["vbitrate"] = arg
if opt in ("-2", "--twopass"): if opt in ("-2", "--twopass"):
log("TwoPass encoding on")
params["twopass"] = True params["twopass"] = True
if opt in ("-r", "--recursive"): if opt in ("-r", "--recursive"):
log("Recursive search of video files")
params["recursive"] = True params["recursive"] = True
def encode_h264(src, dest): def encode_h264(src, dest):
if (params["twopass"]): if (params["twopass"]):
log("two pass encoding started ...") logging.debug("two pass encoding started ...")
firstpass = [ firstpass = [
exec_ffmpeg, exec_ffmpeg,
"-y", "-y",
@ -164,7 +160,7 @@ def encode_h264(src, dest):
subprocess.run(firstpass) subprocess.run(firstpass)
subprocess.run(secondpass) subprocess.run(secondpass)
else: else:
log("one pass encoding started ...") logging.debug("one pass encoding started ...")
encode = [ encode = [
exec_ffmpeg, exec_ffmpeg,
"-i", src, "-i", src,
@ -182,25 +178,25 @@ def encode_h264(src, dest):
if __name__ == "__main__": if __name__ == "__main__":
exec_ffmpeg = find_executable("ffmpeg") exec_ffmpeg = find_executable("ffmpeg")
if not exec_ffmpeg: if not exec_ffmpeg:
log("ffmpeg not found, exiting") print('ffmpeg not found, exiting')
sys.exit(2) sys.exit(2)
# log("export_video v" + VERSION + "FFMPEG exec " + exec_ffmpeg) print('export_video v%s with FFMPEG %s', VERSION, exec_ffmpeg)
processArg(sys.argv[1:]) processArg(sys.argv[1:])
if not params["file_input"]: if not params["file_input"]:
log("you must specify a file / directory" + params["file_input"]) print("you must specify a file / directory input")
sys.exit(2) sys.exit(2)
if os.path.isdir(params["file_input"]): if os.path.isdir(params["file_input"]):
if os.path.isfile(params["file_output"]): if os.path.isfile(params["file_output"]):
log("Can't batch export to a file! Bye Bye") print("Can't batch export to a single file! Bye Bye")
sys.exit(2) sys.exit(2)
log(params["file_input"] + " is a directory") logging.info('%s is a directory', params["file_input"])
video_files = listFile( video_files = listFile(
params["file_input"], params["file_input"],
params["pattern"], params["pattern"],
params["recursive"] params["recursive"]
) )
for f in video_files: for f in video_files:
log("batch encode | file : " + f) logging.info('batch encode | file %s to %s', f)
encode_h264(f, params["file_output"] + getFileName(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"]):
@ -209,9 +205,9 @@ if __name__ == "__main__":
else: else:
if(os.path.isdir(params["file_output"])): if(os.path.isdir(params["file_output"])):
params["file_input"] += getFileName(params["file_input"]) params["file_input"] += getFileName(params["file_input"])
log(params["file_input"] + " is a file") logging.info('%s is a file', params["file_input"])
encode_h264(params["file_input"], params["file_output"]) encode_h264(params["file_input"], params["file_output"])
else: else:
print("input file desn't exist, bye bye") logging.info("input file desn't exist, bye bye")
sys.exit(2) sys.exit(2)
sys.exit(0) sys.exit(0)