From d57fea0040792e7e8acdbb8034d1af29619c22b8 Mon Sep 17 00:00:00 2001 From: ka2er Date: Wed, 17 Oct 2012 23:45:41 +0200 Subject: [PATCH] fix various breaks if file name contains non ascii characters (>127) --- headphones/helpers.py | 8 +++++--- headphones/music_encoder.py | 31 ++++++++++++++++++------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/headphones/helpers.py b/headphones/helpers.py index 211bf637..89fb20cc 100644 --- a/headphones/helpers.py +++ b/headphones/helpers.py @@ -226,11 +226,13 @@ def extract_song_data(s): def smartMove(src, dest, delete=True): + from headphones import logger + source_dir = os.path.dirname(src) filename = os.path.basename(src) if os.path.isfile(os.path.join(dest, filename)): - logger.info('Destination file exists: %s' % os.path.join(dest, filename).decode(headphones.SYS_ENCODING)) + logger.info('Destination file exists: %s' % os.path.join(dest, filename).decode(headphones.SYS_ENCODING, 'replace')) title = os.path.splitext(filename)[0] ext = os.path.splitext(filename)[1] i = 1 @@ -244,7 +246,7 @@ def smartMove(src, dest, delete=True): os.rename(src, os.path.join(source_dir, newfile)) filename = newfile except Exception, e: - logger.warn('Error renaming %s: %s' % (src.decode(headphones.SYS_ENCODING), e)) + logger.warn('Error renaming %s: %s' % (src.decode(headphones.SYS_ENCODING, 'replace'), str(e).decode(headphones.SYS_ENCODING, 'replace'))) break try: @@ -254,4 +256,4 @@ def smartMove(src, dest, delete=True): shutil.copy(os.path.join(source_dir, filename), os.path.join(dest, filename)) return True except Exception, e: - logger.warn('Error moving file %s: %s' % (filename.decode(headphones.SYS_ENCODING), e)) + logger.warn('Error moving file %s: %s' % (filename.decode(headphones.SYS_ENCODING, 'replace'), str(e).decode(headphones.SYS_ENCODING, 'replace'))) diff --git a/headphones/music_encoder.py b/headphones/music_encoder.py index 2cff0505..63e44927 100644 --- a/headphones/music_encoder.py +++ b/headphones/music_encoder.py @@ -66,23 +66,23 @@ def encode(albumPath): for music in musicFiles: infoMusic=MediaFile(music) if headphones.ENCODER == 'lame': - if not any(music.lower().endswith('.' + x) for x in ["mp3", "wav"]): + if not any(music.decode(headphones.SYS_ENCODING, 'replace').lower().endswith('.' + x) for x in ["mp3", "wav"]): logger.warn(u'Lame cant encode "%s" format for "%s", use ffmpeg' % (os.path.splitext(music)[1].decode(headphones.SYS_ENCODING, 'replace'),music.decode(headphones.SYS_ENCODING, 'replace'))) else: - if (music.lower().endswith('.mp3') and (infoMusic.bitrate/1000<=headphones.BITRATE)): + if (music.decode(headphones.SYS_ENCODING, 'replace').lower().endswith('.mp3') and (int(infoMusic.bitrate/1000)<=headphones.BITRATE)): logger.info('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music.decode(headphones.SYS_ENCODING, 'replace'),headphones.BITRATE)) else: command(encoder,music,musicTempFiles[i],albumPath) ifencoded=1 else: if headphones.ENCODEROUTPUTFORMAT=='ogg': - if music.lower().endswith('.ogg'): + if music.decode(headphones.SYS_ENCODING, 'replace').lower().endswith('.ogg'): logger.warn('Can not reencode .ogg music "%s"' % (music.decode(headphones.SYS_ENCODING, 'replace'))) else: command(encoder,music,musicTempFiles[i],albumPath) ifencoded=1 elif (headphones.ENCODEROUTPUTFORMAT=='mp3' or headphones.ENCODEROUTPUTFORMAT=='m4a'): - if (music.lower().endswith('.'+headphones.ENCODEROUTPUTFORMAT) and (infoMusic.bitrate/1000<=headphones.BITRATE)): + if (music.decode(headphones.SYS_ENCODING, 'replace').lower().endswith('.'+headphones.ENCODEROUTPUTFORMAT) and (int(infoMusic.bitrate/1000)<=headphones.BITRATE)): logger.info('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music.decode(headphones.SYS_ENCODING, 'replace'),headphones.BITRATE)) else: command(encoder,music,musicTempFiles[i],albumPath) @@ -102,6 +102,7 @@ def encode(albumPath): return musicFinalFiles def command(encoder,musicSource,musicDest,albumPath): + return_code=1 cmd='' startMusicTime=time.time() @@ -134,15 +135,19 @@ def command(encoder,musicSource,musicDest,albumPath): else: cmd=cmd+' '+ headphones.ADVANCEDENCODER cmd=cmd+ ' "' + musicDest + '"' - print cmd - time.sleep(10) - return_code = call(cmd, shell=True) - if (return_code==0) and (os.path.exists(musicDest)): - if headphones.DELETE_LOSSLESS_FILES: - os.remove(musicSource) - shutil.move(musicDest,albumPath) - logger.info('Music "%s" encoded in %s' % (musicSource,getTimeEncode(startMusicTime))) - + + try: + return_code = call(cmd, shell=True) + + if (return_code==0) and (os.path.exists(musicDest)): + if headphones.DELETE_LOSSLESS_FILES: + os.remove(musicSource) + shutil.move(musicDest,albumPath) + logger.info('Music "%s" encoded in %s' % (musicSource,getTimeEncode(startMusicTime))) + + except subprocess.CalledProcessError, e: + logger.warn('Music "%s" encoding error : %s' % (musicSource, e.output)) + def getTimeEncode(start): seconds =int(time.time()-start) hours = seconds / 3600