From 2dc241f41e6708a354ce739528ae5f41b8ab659a Mon Sep 17 00:00:00 2001 From: pabloalcantara Date: Wed, 17 Aug 2011 14:19:41 -0300 Subject: [PATCH] Verify If mp3 to encode has higher bitrate than new file If has the same o less bitrate, it does not reencode file Fix: Return for headphones the new downloaded_track_list because it has changed when reencoded from other codec --- headphones/encode.py | 45 ++++++++++++++++++++++++------------- headphones/postprocessor.py | 2 +- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/headphones/encode.py b/headphones/encode.py index d5ecec90..06f37100 100644 --- a/headphones/encode.py +++ b/headphones/encode.py @@ -5,12 +5,13 @@ import time from subprocess import call from headphones import logger - +from lib.mutagen.mp3 import MP3 def encode(albumPath): tempDirEncode=os.path.join(albumPath,"temp") musicFiles=[] + musicFinalFiles=[] musicTempFiles=[] encoder ="" if not os.path.exists(tempDirEncode): @@ -38,26 +39,38 @@ def encode(albumPath): if not any(music.endswith('.' + x) for x in ["mp3", "wav"]): logger.warn('Lame cant encode "%s" format for "%s", use ffmpeg' % (os.path.splitext(music)[1],music)) else: - cmd=encoder+' -h --resample ' + str(headphones.SAMPLINGFREQUENCY) + ' -b ' + str(headphones.BITRATE) + if (music.endswith('.mp3') and (MP3(music).info.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+' "'+os.path.join(music)+'"' + cmd=cmd+' "'+os.path.join(musicTempFiles[i])+'"' + return_code = call(cmd, shell=True) + print ('Return_code: ' + str(return_code)) + if return_code==0: + os.remove(music) + shutil.move(musicTempFiles[i],os.path.join(albumPath)) + else: + if (music.endswith('.mp3')): + if ((MP3(music).info.bitrate/1000<=headphones.BITRATE)): + logger.warn('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music,headphones.BITRATE)) + else: + cmd=encoder+' -i' cmd=cmd+' "'+os.path.join(music)+'"' + cmd=cmd+' -ac 2 -vn -ar ' + str(headphones.SAMPLINGFREQUENCY) + ' -ab ' + str(headphones.BITRATE) +'k' cmd=cmd+' "'+os.path.join(musicTempFiles[i])+'"' return_code = call(cmd, shell=True) + print return_code if return_code==0: os.remove(music) shutil.move(musicTempFiles[i],os.path.join(albumPath)) - i=i+1 - else: - cmd=encoder+' -i' - cmd=cmd+' "'+os.path.join(music)+'"' - cmd=cmd+' -ac 2 -vn -ar ' + str(headphones.SAMPLINGFREQUENCY) + ' -ab ' + str(headphones.BITRATE) +'k' - cmd=cmd+' "'+os.path.join(musicTempFiles[i])+'"' - return_code = call(cmd, shell=True) - print return_code - if return_code==0: - os.remove(music) - shutil.move(musicTempFiles[i],os.path.join(albumPath)) - i=i+1 - + i=i+1 + shutil.rmtree(tempDirEncode) time.sleep(1) - logger.info('Encoding for folder "%s" is completed' % (albumPath)) \ No newline at end of file + logger.info('Encoding for folder "%s" is completed' % (albumPath)) + for r,d,f in os.walk(albumPath): + for music in f: + if any(music.endswith('.' + x) for x in headphones.MEDIA_FORMATS): + musicFinalFiles.append(os.path.join(r, music)) + return musicFinalFiles \ No newline at end of file diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 713b8764..7a779e6e 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -199,7 +199,7 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list) logger.info('Starting post-processing for: %s - %s' % (release['ArtistName'], release['AlbumTitle'])) #start enconding if headphones.ENCODE: - encode.encode(albumpath) + downloaded_track_list=encode.encode(albumpath) if headphones.EMBED_ALBUM_ART or headphones.ADD_ALBUM_ART: album_art_path = albumart.getAlbumArt(albumid)