diff --git a/headphones/__init__.py b/headphones/__init__.py index d7865ca2..c2a50ece 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -120,7 +120,8 @@ BITRATE = None SAMPLINGFREQUENCY = None ADVANCEDENCODER = None ENCODEROUTPUTFORMAT = None -VORBISQUALITY = None +ENCODERQUALITY = None +ENCODERVBRCBR = None def CheckSection(sec): """ Check if INI section exists, if not create it """ @@ -180,7 +181,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, ENCODEROUTPUTFORMAT, VORBISQUALITY + ENCODERFOLDER, ENCODER, BITRATE, SAMPLINGFREQUENCY, ENCODE, ADVANCEDENCODER, ENCODEROUTPUTFORMAT, ENCODERQUALITY, ENCODERVBRVBR if __INITIALIZED__: return False @@ -269,7 +270,8 @@ def initialize(): ENCODE = bool(check_setting_int(CFG, 'General', 'encode', 0)) ADVANCEDENCODER = check_setting_str(CFG, 'General', 'advancedencoder', '') ENCODEROUTPUTFORMAT = check_setting_str(CFG, 'General', 'encoderoutputformat', 'mp3') - VORBISQUALITY = check_setting_int(CFG, 'General', 'vorbisquality', 60) + ENCODERQUALITY = check_setting_int(CFG, 'General', 'vorbisquality', 60) + ENCODERVBRCBR = check_setting_str(CFG, 'General', 'encodervbrcbr', 'cbr') if not LOG_DIR: LOG_DIR = os.path.join(DATA_DIR, 'logs') @@ -455,8 +457,9 @@ def config_write(): new_config['General']['encoderfolder'] = ENCODERFOLDER new_config['General']['advancedencoder'] = ADVANCEDENCODER new_config['General']['encoderoutputformat'] = ENCODEROUTPUTFORMAT - new_config['General']['vorbisquality'] = VORBISQUALITY - + new_config['General']['encoderquality'] = ENCODERQUALITY + new_config['General']['encodervbrcbr'] = ENCODERVBRCBR + new_config.write() diff --git a/headphones/encode.py b/headphones/encode.py index 27ec44b3..6211663f 100644 --- a/headphones/encode.py +++ b/headphones/encode.py @@ -13,12 +13,14 @@ except ImportError: import lib.argparse as argparse def encode(albumPath): - tempDirEncode=os.path.join(albumPath,"temp") musicFiles=[] musicFinalFiles=[] musicTempFiles=[] encoder ="" + startAlbumTime=time.clock() + ifencoded=0 + if not os.path.exists(tempDirEncode): os.mkdir(tempDirEncode) else: @@ -30,7 +32,7 @@ def encode(albumPath): for music in f: if any(music.endswith('.' + x) for x in headphones.MEDIA_FORMATS): musicFiles.append(os.path.join(r, music)) - musicTemp = os.path.splitext(music)[0]+'.'+headphones.ENCODEROUTPUTFORMAT + musicTemp = os.path.normpath(os.path.splitext(music)[0]+'.'+headphones.ENCODEROUTPUTFORMAT).encode(headphones.SYS_ENCODING) musicTempFiles.append(os.path.join(tempDirEncode, musicTemp)) if headphones.ENCODER=='lame': @@ -49,33 +51,46 @@ def encode(albumPath): logger.warn('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music,headphones.BITRATE)) else: command(encoder,music,musicTempFiles[i],albumPath) + ifencoded=1 else: if headphones.ENCODEROUTPUTFORMAT=='ogg': if music.endswith('.ogg'): logger.warn('Can not reencode .ogg music "%s"' % (music)) else: command(encoder,music,musicTempFiles[i],albumPath) + ifencoded=1 elif (headphones.ENCODEROUTPUTFORMAT=='mp3' or headphones.ENCODEROUTPUTFORMAT=='m4a'): 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: command(encoder,music,musicTempFiles[i],albumPath) + ifencoded=1 i=i+1 shutil.rmtree(tempDirEncode) - time.sleep(1) - logger.info('Encoding for folder "%s" is completed' % (albumPath)) + time.sleep(1) 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)) + + if ifencoded==0: + logger.info('Encoding for folder "%s" is not needed' % (albumPath)) + else: + logger.info('Encoding for folder "%s" is completed in %s' % (albumPath,getTimeEncode(startAlbumTime))) + return musicFinalFiles def command(encoder,musicSource,musicDest,albumPath): return_code=1 cmd='' + startMusicTime=time.clock() if headphones.ENCODER == 'lame': - cmd=encoder + ' -h --resample ' + str(headphones.SAMPLINGFREQUENCY) + ' -b ' + str(headphones.BITRATE) + cmd=encoder + ' -h' + if headphones.ENCODERVBRCBR=='cbr': + cmd=cmd+ ' --resample ' + str(headphones.SAMPLINGFREQUENCY) + ' -b ' + str(headphones.BITRATE) + elif headphones.ENCODERVBRCBR=='vbr': + cmd=cmd+'' cmd=cmd+ ' ' + headphones.ADVANCEDENCODER cmd=cmd+ ' "' + musicSource + '"' cmd=cmd+ ' "' + musicDest +'"' @@ -86,10 +101,23 @@ def command(encoder,musicSource,musicDest,albumPath): cmd=cmd+ ' -acodec libvorbis' if headphones.ENCODEROUTPUTFORMAT=='m4a': cmd=cmd+ ' -strict experimental' - cmd=cmd+ ' -y -ac 2 -map_metadata 0:0,s0 -vn -ar ' + str(headphones.SAMPLINGFREQUENCY) + ' -ab ' + str(headphones.BITRATE) + 'k' + if headphones.ENCODERVBRCBR=='cbr': + cmd=cmd+ ' -ar ' + str(headphones.SAMPLINGFREQUENCY) + ' -ab ' + str(headphones.BITRATE) + 'k' + elif headphones.ENCODERVBRCBR=='vbr': + cmd=cmd+'' + cmd=cmd+ ' -y -ac 2 -map_metadata 0:0,s0 -vn' cmd=cmd+ ' ' + headphones.ADVANCEDENCODER cmd=cmd+ ' "' + musicDest + '"' return_code = call(cmd, shell=True) if (return_code==0) and (os.path.exists(musicDest)): os.remove(musicSource) - shutil.move(musicDest,albumPath) \ No newline at end of file + shutil.move(musicDest,albumPath) + logger.info('Music "%s" encoded in %s' % (musicSource,getTimeEncode(startMusicTime))) + +def getTimeEncode(start): + seconds =int(time.clock()-start) + hours = seconds / 3600 + seconds -= 3600*hours + minutes = seconds / 60 + seconds -= 60*minutes + return "%02d:%02d:%02d" % (hours, minutes, seconds) \ No newline at end of file