walk through directory here

This commit is contained in:
Clinton Hall 2013-03-13 16:15:55 -07:00
commit 7190394850

View file

@ -6,20 +6,20 @@ from subprocess import call
Logger = logging.getLogger() Logger = logging.getLogger()
def Transcode_file(filePath): def Transcode_file(dirName):
if os.name == 'nt': if os.name == 'nt':
ffmpeg = os.path.join(os.path.dirname(sys.argv[0]), 'ffmpeg\\bin\\ffmpeg.exe') # note, will need to package in this dir. ffmpeg = os.path.join(os.path.dirname(sys.argv[0]), 'ffmpeg\\bin\\ffmpeg.exe') # note, will need to package in this dir.
if not os.path.isfile(ffmpeg): # problem if not os.path.isfile(ffmpeg): # problem
Logger.error("ffmpeg not found. ffmpeg needs to be located at: %s", ffmpeg) Logger.error("ffmpeg not found. ffmpeg needs to be located at: %s", ffmpeg)
Logger.info("Cannot transcode file %s", filePath) Logger.info("Cannot transcode files in folder %s", dirName)
return 1 # failure return 1 # failure
else: else:
if call(['which', ffmpeg]): if call(['which', ffmpeg]):
res = call([os.path.join(os.path.dirname(sys.argv[0]),'getffmpeg.sh')]) res = call([os.path.join(os.path.dirname(sys.argv[0]),'getffmpeg.sh')])
if res or call(['which', ffmpeg]): # did not install or ffmpeg still not found. if res or call(['which', ffmpeg]): # did not install or ffmpeg still not found.
Logger.error("Failed to install ffmpeg. Please install manually") Logger.error("Failed to install ffmpeg. Please install manually")
Logger.info("Cannot transcode file %s", filePath) Logger.info("Cannot transcode files in folder %s", dirName)
return 1 # failure return 1 # failure
else: else:
ffmpeg = 'ffmpeg' ffmpeg = 'ffmpeg'
@ -34,6 +34,7 @@ def Transcode_file(filePath):
config.read(configFilename) config.read(configFilename)
mediaContainer = (config.get("Extensions", "mediaExtensions")).split(',')
duplicate = int(config.get("Transcoder", "duplicate")) duplicate = int(config.get("Transcoder", "duplicate"))
ignoreExtensions = (config.get("Transcoder", "ignoreExtensions")).split(',') ignoreExtensions = (config.get("Transcoder", "ignoreExtensions")).split(',')
outputVideoExtension = config.get("Transcoder", "outputVideoExtension") outputVideoExtension = config.get("Transcoder", "outputVideoExtension")
@ -43,10 +44,16 @@ def Transcode_file(filePath):
outputAudioCodec = config.get("Transcoder", "outputAudioCodec") outputAudioCodec = config.get("Transcoder", "outputAudioCodec")
outputAudioBitrate = config.get("Transcoder", "outputAudioBitrate") outputAudioBitrate = config.get("Transcoder", "outputAudioBitrate")
Logger.info("Checking for files to be transcoded")
final_result = 0 # initialize as successful
for dirpath, dirnames, filenames in os.walk(dirName):
for file in filenames:
filePath = os.path.join(dirpath, file)
name, ext = os.path.splitext(filePath) name, ext = os.path.splitext(filePath)
if ext in mediaContainer: # If the file is a video file
if ext in ignoreExtensions: if ext in ignoreExtensions:
Logger.info("No need to transcode video type %s", ext) Logger.info("No need to transcode video type %s", ext)
return 0 # exit Transcoder. continue
if ext == outputVideoExtension: # we need to change the name to prevent overwriting itself. if ext == outputVideoExtension: # we need to change the name to prevent overwriting itself.
outputVideoExtension = '-transcoded' + outputVideoExtension # adds '-transcoded.ext' outputVideoExtension = '-transcoded' + outputVideoExtension # adds '-transcoded.ext'
newfilePath = os.path.normpath(name + outputVideoExtension) newfilePath = os.path.normpath(name + outputVideoExtension)
@ -81,4 +88,6 @@ def Transcode_file(filePath):
os.unlink(filePath) os.unlink(filePath)
else: else:
Logger.error("Transcoding of video %s to %s failed", filePath, newfilePath) Logger.error("Transcoding of video %s to %s failed", filePath, newfilePath)
return result # this will be 0 (successful) it all are sucessful, else will return a positive integer for failure.
final_result = final_result + result
return final_result