Guest User

Untitled

a guest
Jun 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """A utility for creating a clip of an mp3 file"""
  3.  
  4. import getopt, os, sys
  5. from subprocess import Popen, PIPE
  6. from mutagen.id3 import ID3
  7.  
  8.  
  9. def copyid3(src, dst):
  10. """ Copy the id3 tag from the src file to the dst file """
  11. id3 = ID3(src)
  12. id3.save(dst, v1=0)
  13.  
  14. def mp3towav(mp3filename, wavfilename):
  15. """ Convert mp3filename to a wav file, wavfilename """
  16. pid = Popen(["mpg123", "-w", wavfilename, mp3filename], stdout=PIPE,
  17. stderr=PIPE)
  18. return pid.communicate()[0]
  19.  
  20. def cutwav(wavfilename, cutwavfilename, endtime):
  21. """ Cut wavfilename """
  22. pid = Popen(["qwavcut", "-s", endtime, "-o", cutwavfilename, wavfilename],
  23. stderr=PIPE, stdout=PIPE)
  24. return pid.communicate()[0]
  25.  
  26. def fadeoutwav(wavfilename, duration):
  27. """ Fade out wavfilename by duration """
  28. pid = Popen(["qwavfade", "-l", duration, "-o", wavfilename],
  29. stderr=PIPE, stdout=PIPE)
  30. return pid.communicate()[0]
  31.  
  32. def encodemp3(wavfilename, mp3filename, bitrate):
  33. """ Encode wav file wavfilename into an mp3, mp3filename """
  34. pid = Popen(["lame", "-b", bitrate, wavfilename, mp3filename],
  35. stderr=PIPE, stdout=PIPE)
  36. return pid.communicate()[0]
  37.  
  38. def run(outdir, endtime, fadeout, files):
  39. """
  40. Process "files". Fade them out by "fadeout" time after "endTime"
  41. Copy resulting samples to outdir
  42. """
  43. #make new directory to store them in
  44. if outdir != None:
  45. if not os.path.isdir(outdir):
  46. os.mkdir(outdir)
  47.  
  48. #For each file
  49. for mp3filename in files:
  50. mp3filename = os.path.abspath(mp3filename)
  51. baseabsname, ext = os.path.splitext(mp3filename)
  52. basedir, basename = os.path.split(mp3filename)
  53. basename = os.path.splitext(basename)[0]
  54. #make sure it's an mp3
  55. if ext != '.mp3':
  56. break
  57.  
  58. wavfilename = baseabsname+'.wav'
  59. if os.path.isfile(wavfilename):
  60. print "%s already exists?" % (wavfilename)
  61. return
  62. try:
  63. print "Working on %s" % (basename)
  64. #convert to wav
  65. mp3towav(mp3filename, wavfilename)
  66.  
  67. #cut the wav (overwrite original is fine)
  68. cutwavfilename = baseabsname+'_cut_'+'.wav'
  69. cutwav(wavfilename, cutwavfilename, endtime)
  70.  
  71. #fade it (overwrite original is fine)
  72. fadeoutwav(cutwavfilename, fadeout)
  73.  
  74. #encode temp wav to mp3 in the new directory
  75. newmp3filename = os.path.join(outdir if outdir else basedir,
  76. basename+'_sample.mp3')
  77. encodemp3(cutwavfilename, newmp3filename, '128')
  78.  
  79. #insert id3 object
  80. copyid3(mp3filename, newmp3filename)
  81. finally:
  82. os.remove(wavfilename)
  83. os.remove(cutwavfilename)
  84.  
  85.  
  86. def usage():
  87. """ print out usage for command line use """
  88. print "createMP3Snippet.py [-o|--outdir <output directory>] [-h|--help]"
  89. print " [-c|--cut <point to cut off>]"
  90. print " [-f|--fadeout <fade out duration>]"
  91. print " <file pattern to process>"
  92.  
  93. def main():
  94. """ main program, only in here should we call sys.exit """
  95. try:
  96. opts, args = getopt.getopt(sys.argv[1:],
  97. "o:hc:f:", ["outdir", "help", "cut", "fadeout"])
  98. except getopt.GetoptError, err:
  99. print str(err)
  100. usage()
  101. sys.exit(2)
  102. outdir = None
  103. cut = "30s"
  104. fadeout = "5s"
  105. for option, argument in opts:
  106. if option in ("-o", "--outdir"):
  107. outdir = argument
  108. elif option in ("-h", "--help"):
  109. usage()
  110. sys.exit()
  111. elif option in ("-c", "--cut"):
  112. cut = argument
  113. elif option in ("-f", "--fadeout"):
  114. fadeout = argument
  115. else:
  116. assert False, "unhandled option"
  117. run(outdir, cut, fadeout, args)
  118.  
  119. if __name__ == "__main__":
  120. main()
Add Comment
Please, Sign In to add comment