Advertisement
overloop

audiobook_split.py

Dec 15th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import re
  5. import sys
  6. import subprocess
  7. import os
  8. import math
  9. import glob
  10.  
  11. if len(sys.argv)<2:
  12.     print "pass arguments"
  13.     sys.exit(0)
  14.  
  15. my_env = os.environ
  16. my_env['PYTHONIOENCODING'] = 'utf-8'
  17.  
  18. def timeStrInt(time):
  19.     m = re.search('([0-9]{2}):([0-9]{2}):([0-9]{2})',time)
  20.     if m == None:
  21.         return -1
  22.     return int(m.group(1))*3600+int(m.group(2))*60+int(m.group(3))
  23.  
  24. def timeIntStr(time):
  25.     h = math.floor(time/3600)
  26.     time = time - h*3600
  27.     m = math.floor(time/60)
  28.     time = time - m*60
  29.     s = time
  30.     return "%02d:%02d:%02d" % (h,m,s)
  31.  
  32. def ffmpegDuration(file):
  33.     proc = subprocess.Popen(['ffmpeg','-i',file],
  34.                         stdout=subprocess.PIPE,
  35.                         stderr=subprocess.PIPE,
  36.                         )
  37.     stdout_value, stderr_value = proc.communicate('')
  38.     audioinfo = repr(stderr_value)
  39.     m = re.search('Duration: ([0-9:]{8})',audioinfo)
  40.     duration = timeStrInt(m.group(1))
  41.     return duration
  42.  
  43. def ffmpegCut(filein,start,duration,artist,title,fileout):
  44.     args = ['ffmpeg','-i',filein,'-ss',timeIntStr(start),'-t',timeIntStr(duration),'-acodec','copy','-metadata','artist="' + artist + '"','-metadata','title="' + title + '"',fileout]
  45.     #print " ".join(args)
  46.     proc = subprocess.call(args,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
  47.  
  48. def getTitle(filename):
  49.     filename = os.path.basename(filename).replace(".mp3","")
  50.     filename1 = filename.split(" – ")
  51.     filename2 = filename.split(" - ")
  52.     if len(filename1) == 2:
  53.         return filename1[1]
  54.     if len(filename2) == 2:
  55.         return filename2[1]
  56.     return filename
  57.  
  58. def getArtist(filename):
  59.     filename = os.path.basename(filename).replace(".mp3","")
  60.     filename1 = filename.split(" – ")
  61.     filename2 = filename.split(" - ")
  62.     if len(filename1) == 2:
  63.         return filename1[0]
  64.     if len(filename2) == 2:
  65.         return filename2[0]
  66.     return ""
  67.  
  68. slice = 10 * 60
  69. sync = 6
  70. for i in range(len(sys.argv)-1):
  71.     fileins = glob.glob(sys.argv[i+1]);
  72.     for filein in fileins:
  73.         print filein
  74.         duration = ffmpegDuration(filein)
  75.         m = re.search('(.*)\.mp3',filein)
  76.         fileoutbase = m.group(1)
  77.         for i in range(int(math.ceil(duration/slice))+1):
  78.             fileout = fileoutbase + "-%02d.mp3" % (i+1)
  79.             artist = getArtist(fileout)
  80.             title = getTitle(fileout)
  81.             start = i*slice
  82.             duration = slice
  83.             if i>0:
  84.                 start = start - sync / 2
  85.                 duration = duration + sync
  86.             elif i==0:
  87.                 duration = duration + sync / 2
  88.             #print title
  89.             ffmpegCut(filein,start,duration,artist,title,fileout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement