/headphones/encode.py

https://github.com/PierreAronnax/headphones · Python · 136 lines · 124 code · 12 blank · 0 comment · 50 complexity · d34c312c1d0f2b296d57c98744c6a2f2 MD5 · raw file

  1. import os
  2. import headphones
  3. import shutil
  4. import time
  5. from subprocess import call
  6. from headphones import logger
  7. from lib.beets.mediafile import MediaFile
  8. try:
  9. import argparse
  10. except ImportError:
  11. import lib.argparse as argparse
  12. def encode(albumPath):
  13. tempDirEncode=os.path.join(albumPath,"temp")
  14. musicFiles=[]
  15. musicFinalFiles=[]
  16. musicTempFiles=[]
  17. encoder =""
  18. startAlbumTime=time.clock()
  19. ifencoded=0
  20. if not os.path.exists(tempDirEncode):
  21. os.mkdir(tempDirEncode)
  22. else:
  23. shutil.rmtree(tempDirEncode)
  24. time.sleep(1)
  25. os.mkdir(tempDirEncode)
  26. for r,d,f in os.walk(albumPath):
  27. for music in f:
  28. if any(music.lower().endswith('.' + x.lower()) for x in headphones.MEDIA_FORMATS):
  29. if (headphones.ENCODERLOSSLESS):
  30. if (music.lower().endswith('.flac')):
  31. musicFiles.append(os.path.join(r, music))
  32. musicTemp = os.path.normpath(os.path.splitext(music)[0]+'.'+headphones.ENCODEROUTPUTFORMAT).encode(headphones.SYS_ENCODING)
  33. musicTempFiles.append(os.path.join(tempDirEncode, musicTemp))
  34. else:
  35. logger.warn('Music "%s" is already encoded' % (music))
  36. else:
  37. musicFiles.append(os.path.join(r, music))
  38. musicTemp = os.path.normpath(os.path.splitext(music)[0]+'.'+headphones.ENCODEROUTPUTFORMAT).encode(headphones.SYS_ENCODING)
  39. musicTempFiles.append(os.path.join(tempDirEncode, musicTemp))
  40. if headphones.ENCODER=='lame':
  41. encoder=os.path.join(headphones.ENCODERFOLDER,'lame')
  42. elif headphones.ENCODER=='ffmpeg':
  43. encoder=os.path.join(headphones.ENCODERFOLDER,'ffmpeg')
  44. i=0
  45. for music in musicFiles:
  46. infoMusic=MediaFile(music)
  47. if headphones.ENCODER == 'lame':
  48. if not any(music.lower().endswith('.' + x) for x in ["mp3", "wav"]):
  49. logger.warn('Lame cant encode "%s" format for "%s", use ffmpeg' % (os.path.splitext(music)[1],music))
  50. else:
  51. if (music.lower().endswith('.mp3') and (infoMusic.bitrate/1000<=headphones.BITRATE)):
  52. logger.warn('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music,headphones.BITRATE))
  53. else:
  54. command(encoder,music,musicTempFiles[i],albumPath)
  55. ifencoded=1
  56. else:
  57. if headphones.ENCODEROUTPUTFORMAT=='ogg':
  58. if music.lower().endswith('.ogg'):
  59. logger.warn('Can not reencode .ogg music "%s"' % (music))
  60. else:
  61. command(encoder,music,musicTempFiles[i],albumPath)
  62. ifencoded=1
  63. elif (headphones.ENCODEROUTPUTFORMAT=='mp3' or headphones.ENCODEROUTPUTFORMAT=='m4a'):
  64. if (music.lower().endswith('.'+headphones.ENCODEROUTPUTFORMAT) and (infoMusic.bitrate/1000<=headphones.BITRATE)):
  65. logger.warn('Music "%s" has bitrate<="%skbit" will not be reencoded' % (music,headphones.BITRATE))
  66. else:
  67. command(encoder,music,musicTempFiles[i],albumPath)
  68. ifencoded=1
  69. i=i+1
  70. shutil.rmtree(tempDirEncode)
  71. time.sleep(1)
  72. for r,d,f in os.walk(albumPath):
  73. for music in f:
  74. if any(music.lower().endswith('.' + x.lower()) for x in headphones.MEDIA_FORMATS):
  75. musicFinalFiles.append(os.path.join(r, music))
  76. if ifencoded==0:
  77. logger.info('Encoding for folder "%s" is not needed' % (albumPath))
  78. return musicFinalFiles
  79. def command(encoder,musicSource,musicDest,albumPath):
  80. return_code=1
  81. cmd=''
  82. startMusicTime=time.clock()
  83. if headphones.ENCODER == 'lame':
  84. if headphones.ADVANCEDENCODER =='':
  85. cmd=encoder + ' -h'
  86. if headphones.ENCODERVBRCBR=='cbr':
  87. cmd=cmd+ ' --resample ' + str(headphones.SAMPLINGFREQUENCY) + ' -b ' + str(headphones.BITRATE)
  88. elif headphones.ENCODERVBRCBR=='vbr':
  89. cmd=cmd+' -V'+str(headphones.ENCODERQUALITY)
  90. cmd=cmd+ ' ' + headphones.ADVANCEDENCODER
  91. else:
  92. cmd=cmd+' '+ headphones.ADVANCEDENCODER
  93. cmd=cmd+ ' "' + musicSource + '"'
  94. cmd=cmd+ ' "' + musicDest +'"'
  95. elif headphones.ENCODER == 'ffmpeg':
  96. cmd=encoder+ ' -i'
  97. cmd=cmd+ ' "' + musicSource + '"'
  98. if headphones.ADVANCEDENCODER =='':
  99. if headphones.ENCODEROUTPUTFORMAT=='ogg':
  100. cmd=cmd+ ' -acodec libvorbis'
  101. if headphones.ENCODEROUTPUTFORMAT=='m4a':
  102. cmd=cmd+ ' -strict experimental'
  103. if headphones.ENCODERVBRCBR=='cbr':
  104. cmd=cmd+ ' -ar ' + str(headphones.SAMPLINGFREQUENCY) + ' -ab ' + str(headphones.BITRATE) + 'k'
  105. elif headphones.ENCODERVBRCBR=='vbr':
  106. cmd=cmd+' -aq ' + str(headphones.ENCODERQUALITY)
  107. cmd=cmd+ ' -y -ac 2 -map_meta_data 0:0,s0 -vn'
  108. else:
  109. cmd=cmd+' '+ headphones.ADVANCEDENCODER
  110. cmd=cmd+ ' "' + musicDest + '"'
  111. print cmd
  112. time.sleep(10)
  113. return_code = call(cmd, shell=True)
  114. if (return_code==0) and (os.path.exists(musicDest)):
  115. os.remove(musicSource)
  116. shutil.move(musicDest,albumPath)
  117. logger.info('Music "%s" encoded in %s' % (musicSource,getTimeEncode(startMusicTime)))
  118. def getTimeEncode(start):
  119. seconds =int(time.clock()-start)
  120. hours = seconds / 3600
  121. seconds -= 3600*hours
  122. minutes = seconds / 60
  123. seconds -= 60*minutes
  124. return "%02d:%02d:%02d" % (hours, minutes, seconds)