From 776f67bd4641a4a49c01d9e45529046512cb3722 Mon Sep 17 00:00:00 2001 From: pabloalcantara Date: Thu, 18 Aug 2011 15:27:17 -0300 Subject: [PATCH] Initial suport to permit encode to others formats --- headphones/__init__.py | 7 ++++-- headphones/encode.py | 53 ++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/headphones/__init__.py b/headphones/__init__.py index 34033bfe..e362c87d 100755 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -118,6 +118,7 @@ ENCODER = None BITRATE = None SAMPLINGFREQUENCY = None ADVANCEDENCODER = None +ENCODEROUTPUTFORMAT = None def CheckSection(sec): """ Check if INI section exists, if not create it """ @@ -177,7 +178,7 @@ def initialize(): LIBRARYSCAN_INTERVAL, DOWNLOAD_SCAN_INTERVAL, SAB_HOST, SAB_USERNAME, SAB_PASSWORD, SAB_APIKEY, SAB_CATEGORY, \ NZBMATRIX, NZBMATRIX_USERNAME, NZBMATRIX_APIKEY, NEWZNAB, NEWZNAB_HOST, NEWZNAB_APIKEY, \ NZBSORG, NZBSORG_UID, NZBSORG_HASH, NEWZBIN, NEWZBIN_UID, NEWZBIN_PASSWORD, LASTFM_USERNAME, INTERFACE, FOLDER_PERMISSIONS, \ - ENCODERFOLDER, ENCODER, BITRATE, SAMPLINGFREQUENCY, ENCODE, ADVANCEDENCODER + ENCODERFOLDER, ENCODER, BITRATE, SAMPLINGFREQUENCY, ENCODE, ADVANCEDENCODER, ENCODEROUTPUTFORMAT if __INITIALIZED__: return False @@ -264,7 +265,8 @@ def initialize(): SAMPLINGFREQUENCY= check_setting_int(CFG, 'General', 'samplingfrequency', 44100) ENCODE = bool(check_setting_int(CFG, 'General', 'encode', 0)) ADVANCEDENCODER = check_setting_int(CFG, 'General', 'advancedencoder', '') - + ENCODEROUTPUTFORMAT = check_setting_int(CFG, 'General', 'encoderoutputformat', 'mp3') + if not LOG_DIR: LOG_DIR = os.path.join(DATA_DIR, 'logs') @@ -444,6 +446,7 @@ def config_write(): new_config['General']['samplingfrequency'] = SAMPLINGFREQUENCY new_config['General']['encoderfolder'] = ENCODERFOLDER new_config['General']['advancedencoder'] = ADVANCEDENCODER + new_config['General']['encoderoutputformat'] = ENCODEROUTPUTFORMAT new_config.write() diff --git a/headphones/encode.py b/headphones/encode.py index 8f2e5df1..686c6a94 100644 --- a/headphones/encode.py +++ b/headphones/encode.py @@ -30,46 +30,29 @@ def encode(albumPath): for music in f: if any(music.endswith('.' + x) for x in ["mp3", "flac", "m4a", "wav"]): musicFiles.append(os.path.join(r, music)) - musicTemp = os.path.join(os.path.splitext(music)[0])+'.mp3' + musicTemp = os.path.splitext(music)[0]+'.'+headphones.ENCODEROUTPUTFORMAT musicTempFiles.append(os.path.join(tempDirEncode, musicTemp)) if headphones.ENCODER=='lame': encoder=os.path.join(headphones.ENCODERFOLDER,'lame') - else: + elif headphones.ENCODER=='ffmpeg': encoder=os.path.join(headphones.ENCODERFOLDER,'ffmpeg') i=0 - for music in musicFiles: - return_code=1 + for music in musicFiles: infoMusic=MediaFile(music) if headphones.ENCODER == 'lame': - if not any(music.endswith('.' + x) for x in ["mp3", "wav"]): + if not any(music.endswith('.' +headphones.ENCODEROUTPUTFORMAT) for x in ["mp3", "wav"]): logger.warn('Lame cant encode "%s" format for "%s", use ffmpeg' % (os.path.splitext(music)[1],music)) else: if (music.endswith('.mp3') and (infoMusic.bitrate/1000<=headphones.BITRATE)): logger.warn('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music,headphones.BITRATE)) else: - cmd=encoder + ' -h --resample ' + str(headphones.SAMPLINGFREQUENCY) + ' -b ' + str(headphones.BITRATE) - cmd=cmd+ ' ' + headphones.ADVANCEDENCODER - cmd=cmd+ ' "' + music + '"' - cmd=cmd+ ' "' + musicTempFiles[i] +'"' - return_code = call(cmd, shell=True) - if return_code==0: - os.remove(music) - shutil.move(musicTempFiles[i],albumPath) - else: - if (music.endswith('.mp3') and (infoMusic.bitrate/1000<=headphones.BITRATE)): + command(encoder,music,musicTempFiles[i],albumPath) + elif headphones.ENCODER == 'ffmpeg': + if (music.endswith('.'+headphones.ENCODEROUTPUTFORMAT) and (infoMusic.bitrate/1000<=headphones.BITRATE)): logger.warn('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music,headphones.BITRATE)) else: - cmd=encoder+ ' -i' - cmd=cmd+ ' "' + music + '"' - cmd=cmd+ ' -ac 2 -vn -ar ' + str(headphones.SAMPLINGFREQUENCY) + ' -ab ' + str(headphones.BITRATE) + 'k' - cmd=cmd+ ' ' + headphones.ADVANCEDENCODER - cmd=cmd+ ' "' + musicTempFiles[i] + '"' - return_code = call(cmd, shell=True) - if return_code==0: - os.remove(music) - shutil.move(musicTempFiles[i],albumPath) - + command(encoder,music,musicTempFiles[i],albumPath) i=i+1 shutil.rmtree(tempDirEncode) @@ -80,3 +63,23 @@ def encode(albumPath): if any(music.endswith('.' + x) for x in headphones.MEDIA_FORMATS): musicFinalFiles.append(os.path.join(r, music)) return musicFinalFiles + +def command(encoder,musicSource,musicDest,albumPath): + return_code=1 + if headphones.ENCODER == 'lame': + cmd=encoder + ' -h --resample ' + str(headphones.SAMPLINGFREQUENCY) + ' -b ' + str(headphones.BITRATE) + cmd=cmd+ ' ' + headphones.ADVANCEDENCODER + cmd=cmd+ ' "' + musicSource + '"' + cmd=cmd+ ' "' + musicDest +'"' + elif headphones.ENCODER == 'ffmpeg': + if headphones.ENCODEROUTPUTFORMAT=='mp3': + cmd=encoder+ ' -i' + cmd=cmd+ ' "' + musicSource + '"' + cmd=cmd+ ' -ac 2 -map_metadata 0:0,s0 -vn -ar ' + str(headphones.SAMPLINGFREQUENCY) + ' -ab ' + str(headphones.BITRATE) + 'k' + cmd=cmd+ ' ' + headphones.ADVANCEDENCODER + cmd=cmd+ ' "' + musicDest + '"' + + return_code = call(cmd, shell=True) + if return_code==0: + os.remove(musicSource) + shutil.move(musicDest,albumPath) \ No newline at end of file