From 1a6714c8cffb3fb80c68c2dd6d64857607b2c096 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 17 Aug 2011 00:17:25 -0300 Subject: [PATCH 1/2] Beta function: MP3 reencode Initial stage --- headphones/__init__.py | 27 ++++++++++++++++++++++++--- headphones/postprocessor.py | 8 +++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/headphones/__init__.py b/headphones/__init__.py index 7b1fc2fb..7a1f5dbf 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -107,6 +107,12 @@ MEDIA_FORMATS = ["mp3", "flac", "aac", "ogg", "ape", "m4a"] INTERFACE = None FOLDER_PERMISSIONS = None +ENCODE = None +ENCODERFOLDER = None +ENCODER = None +BITRATE = None +SAMPLINGFREQUENCY = None + def CheckSection(sec): """ Check if INI section exists, if not create it """ try: @@ -164,7 +170,8 @@ def initialize(): ADD_ALBUM_ART, EMBED_ALBUM_ART, DOWNLOAD_DIR, BLACKHOLE, BLACKHOLE_DIR, USENET_RETENTION, NZB_SEARCH_INTERVAL, \ 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 + NZBSORG, NZBSORG_UID, NZBSORG_HASH, NEWZBIN, NEWZBIN_UID, NEWZBIN_PASSWORD, LASTFM_USERNAME, INTERFACE, FOLDER_PERMISSIONS, \ + ENCODERFOLDER, ENCODER, BITRATE, SAMPLINGFREQUENCY, ENCODE if __INITIALIZED__: return False @@ -243,7 +250,15 @@ def initialize(): INTERFACE = check_setting_str(CFG, 'General', 'interface', 'default') FOLDER_PERMISSIONS = check_setting_str(CFG, 'General', 'folder_permissions', '0755') - + + ENCODERFOLDER = check_setting_str(CFG, 'General', 'encoderfolder', '') + ENCODER = check_setting_str(CFG, 'General', 'encoder', 'ffmpeg') + BITRATE = check_setting_str(CFG, 'General', 'bitrate', '128') + SAMPLINGFREQUENCY= check_setting_str(CFG, 'General', 'samplingfrequency', '44100') + ENCODE = check_setting_str(CFG, 'General', 'encode', 'false') + + + if not LOG_DIR: LOG_DIR = os.path.join(DATA_DIR, 'logs') @@ -411,7 +426,13 @@ def config_write(): new_config['General']['lastfm_username'] = LASTFM_USERNAME new_config['General']['interface'] = INTERFACE new_config['General']['folder_permissions'] = FOLDER_PERMISSIONS - + + new_config['General']['encode'] = ENCODE + new_config['General']['encoder'] = ENCODER + new_config['General']['bitrate'] = BITRATE + new_config['General']['samplingfrequency'] = SAMPLINGFREQUENCY + new_config['General']['encoderfolder'] = ENCODERFOLDER + new_config.write() diff --git a/headphones/postprocessor.py b/headphones/postprocessor.py index 8bf96692..89b863f6 100644 --- a/headphones/postprocessor.py +++ b/headphones/postprocessor.py @@ -1,8 +1,7 @@ import os import time - +import encode import urllib, shutil, re - import lib.beets as beets from lib.beets import autotag from lib.beets.mediafile import MediaFile @@ -198,7 +197,10 @@ def verify(albumid, albumpath): 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=='1': + encode.encode(albumpath) + if headphones.EMBED_ALBUM_ART or headphones.ADD_ALBUM_ART: album_art_path = albumart.getAlbumArt(albumid) From 607657dbf8b5e77824853c9e922c9524e4f1ec6f Mon Sep 17 00:00:00 2001 From: pabloalcantara Date: Wed, 17 Aug 2011 00:46:33 -0300 Subject: [PATCH 2/2] Forget to add encode.py --- headphones/encode.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 headphones/encode.py diff --git a/headphones/encode.py b/headphones/encode.py new file mode 100644 index 00000000..a07a9aef --- /dev/null +++ b/headphones/encode.py @@ -0,0 +1,56 @@ +import os +import headphones +import argparse +import shutil +import time + +from subprocess import call + +def encode(albumPath): + + tempDirEncode=os.path.join(albumPath,"temp") + musicFiles=[] + musicTempFiles=[] + encoder ="" + if not os.path.exists(tempDirEncode): + os.mkdir(tempDirEncode) + else: + shutil.rmtree(tempDirEncode) + time.sleep(1) + os.mkdir(tempDirEncode) + + for r,d,f in os.walk(albumPath): + for music in f: + if any(music.endswith('.' + x) for x in headphones.MEDIA_FORMATS): + musicFiles.append(os.path.join(r, music)) + musicTempFiles.append(os.path.join(tempDirEncode, music)) + + if headphones.ENCODER=='lame': + encoder=os.path.join(headphones.ENCODERFOLDER,'lame') + else: + encoder=os.path.join(headphones.ENCODERFOLDER,'ffmpeg') + i=0 + for music in musicFiles: + return_code=1 + if headphones.ENCODER == 'lame': + cmd=encoder+' -h --resample ' + headphones.SAMPLINGFREQUENCY + ' -b ' + headphones.BITRATE + cmd=cmd+' "'+os.path.join(music)+'"' + cmd=cmd+' "'+os.path.join(musicTempFiles[i])+'"' + return_code = call(cmd, shell=True) + if return_code==0: + os.remove(music) + os.rename(musicTempFiles[i],music) + i=i+1 + else: + cmd=encoder+' -i' + cmd=cmd+' "'+os.path.join(music)+'"' + cmd=cmd+' -ac 2 -vn -ar ' + headphones.SAMPLINGFREQUENCY + ' -ab ' + 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) + os.rename(musicTempFiles[i],music) + i=i+1 + + shutil.rmtree(tempDirEncode) \ No newline at end of file