Code review with pylint (not complete)
This commit is contained in:
parent
f7d8edba6a
commit
6d9c0da66a
1 changed files with 47 additions and 22 deletions
|
@ -1,10 +1,8 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
"""
|
||||||
# export-video.py
|
export-video
|
||||||
# ---------------
|
Little scrip to export video file for The Internet :D
|
||||||
# little python script to encode vide file for the web
|
"""
|
||||||
#
|
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import getopt
|
import getopt
|
||||||
import re
|
import re
|
||||||
|
@ -15,6 +13,7 @@ logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
|
""" Print usage"""
|
||||||
print("export-video -i input -o output [-d --desinterlace]")
|
print("export-video -i input -o output [-d --desinterlace]")
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
|
@ -66,24 +65,38 @@ def execute(command, shell=False):
|
||||||
|
|
||||||
|
|
||||||
def find_executable(exec_name):
|
def find_executable(exec_name):
|
||||||
|
"""
|
||||||
|
find executable with which command
|
||||||
|
exec_name (string) : executable to find
|
||||||
|
"""
|
||||||
if not exec_name:
|
if not exec_name:
|
||||||
raise ValueError("find_executable: exec_name required")
|
raise ValueError("find_executable: exec_name required")
|
||||||
return execute(['which', exec_name])
|
return execute(['which', exec_name])
|
||||||
|
|
||||||
|
|
||||||
def listFile(directory, pattern, recursive):
|
def list_file(directory, pattern, recursive):
|
||||||
|
"""
|
||||||
|
list files from a directory
|
||||||
|
directory (string) : source directory
|
||||||
|
pattern (string) : regex used to found files
|
||||||
|
recursive (boolean) : should i serach on sub-folder?
|
||||||
|
"""
|
||||||
logging.info('searching in directory %s', directory)
|
logging.info('searching in directory %s', directory)
|
||||||
files_list = []
|
files_list = []
|
||||||
try:
|
try:
|
||||||
dirs = os.listdir(directory)
|
dirs = os.listdir(directory)
|
||||||
except:
|
except OSError:
|
||||||
logging.error('error : %s not a directory?', 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)
|
logging.info('directory found : %s', i)
|
||||||
files_list.extend(listFile(directory + "/" + i, pattern))
|
files_list.extend(list_file(
|
||||||
|
directory + "/" + i,
|
||||||
|
pattern,
|
||||||
|
recursive
|
||||||
|
))
|
||||||
else:
|
else:
|
||||||
if re.search(pattern, i) is not None:
|
if re.search(pattern, i) is not None:
|
||||||
logging.info('file found : %s', i)
|
logging.info('file found : %s', i)
|
||||||
|
@ -92,10 +105,18 @@ def listFile(directory, pattern, recursive):
|
||||||
|
|
||||||
|
|
||||||
def get_file_name(path):
|
def get_file_name(path):
|
||||||
|
"""
|
||||||
|
find basename without extension
|
||||||
|
path (string) : complete path + file
|
||||||
|
"""
|
||||||
return os.path.splitext(os.path.basename(path))[0]
|
return os.path.splitext(os.path.basename(path))[0]
|
||||||
|
|
||||||
|
|
||||||
def process_arg(sysarg):
|
def process_arg(sysarg):
|
||||||
|
"""
|
||||||
|
Process arguments
|
||||||
|
sysarg (list) : list of arguments
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(
|
opts, args = getopt.getopt(
|
||||||
sysarg, "hi:o:dp:a:v:2",
|
sysarg, "hi:o:dp:a:v:2",
|
||||||
|
@ -138,10 +159,14 @@ def process_arg(sysarg):
|
||||||
|
|
||||||
|
|
||||||
def encode_h264(src, dest):
|
def encode_h264(src, dest):
|
||||||
if (params["twopass"]):
|
""" Encode file
|
||||||
|
src (string) : source video file
|
||||||
|
dest (string) : destination file
|
||||||
|
"""
|
||||||
|
if params["twopass"]:
|
||||||
logging.debug("two pass encoding started ...")
|
logging.debug("two pass encoding started ...")
|
||||||
firstpass = [
|
firstpass = [
|
||||||
exec_ffmpeg,
|
EXEC_FFMPEG,
|
||||||
"-y",
|
"-y",
|
||||||
"-i", src,
|
"-i", src,
|
||||||
"-codec:va", "libx264",
|
"-codec:va", "libx264",
|
||||||
|
@ -152,7 +177,7 @@ def encode_h264(src, dest):
|
||||||
"/dev/null"
|
"/dev/null"
|
||||||
]
|
]
|
||||||
secondpass = [
|
secondpass = [
|
||||||
exec_ffmpeg,
|
EXEC_FFMPEG,
|
||||||
"-i", src,
|
"-i", src,
|
||||||
"-pass", "2",
|
"-pass", "2",
|
||||||
"-codec:v", "libx264",
|
"-codec:v", "libx264",
|
||||||
|
@ -167,7 +192,7 @@ def encode_h264(src, dest):
|
||||||
else:
|
else:
|
||||||
logging.debug("one pass encoding started ...")
|
logging.debug("one pass encoding started ...")
|
||||||
encode = [
|
encode = [
|
||||||
exec_ffmpeg,
|
EXEC_FFMPEG,
|
||||||
"-i", src,
|
"-i", src,
|
||||||
"-codec:v", "libx264",
|
"-codec:v", "libx264",
|
||||||
"-b:v ", params["vbitrate"]
|
"-b:v ", params["vbitrate"]
|
||||||
|
@ -181,8 +206,8 @@ 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:
|
||||||
print('ffmpeg not found, exiting')
|
print('ffmpeg not found, exiting')
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
process_arg(sys.argv[1:])
|
process_arg(sys.argv[1:])
|
||||||
|
@ -194,20 +219,20 @@ if __name__ == "__main__":
|
||||||
print("Can't batch export to a single file! Bye Bye")
|
print("Can't batch export to a single file! Bye Bye")
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
logging.info('%s is a directory', params["file_input"])
|
logging.info('%s is a directory', params["file_input"])
|
||||||
video_files = listFile(
|
video_files = list_file(
|
||||||
params["file_input"],
|
params["file_input"],
|
||||||
params["pattern"],
|
params["pattern"],
|
||||||
params["recursive"]
|
params["recursive"]
|
||||||
)
|
)
|
||||||
for f in video_files:
|
for f in video_files:
|
||||||
logging.info('batch encode | file %s to %s', f)
|
logging.info('batch encode | file %s', f)
|
||||||
encode_h264(f, params["file_output"] + get_file_name(f))
|
encode_h264(f, params["file_output"] + get_file_name(f))
|
||||||
else:
|
else:
|
||||||
if os.path.isfile(params["file_input"]):
|
if os.path.isfile(params["file_input"]):
|
||||||
if not params["file_output"]:
|
if not params["file_output"]:
|
||||||
params["file_output"] = get_file_name(params["file_input"])
|
params["file_output"] = get_file_name(params["file_input"])
|
||||||
else:
|
else:
|
||||||
if(os.path.isdir(params["file_output"])):
|
if os.path.isdir(params["file_output"]):
|
||||||
params["file_input"] += get_file_name(params["file_input"])
|
params["file_input"] += get_file_name(params["file_input"])
|
||||||
logging.info('%s is a file', params["file_input"])
|
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"])
|
||||||
|
|
Reference in a new issue