Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. import os
  2. import tempfile
  3. import subprocess
  4.  
  5. from twisted.internet import defer
  6. from twisted.internet.utils import getProcessOutputAndValue
  7.  
  8.  
  9. class AudioConversionError(Exception): pass
  10.  
  11.  
  12. def _is_exe(fname):
  13.     return os.path.isfile(fname) and os.access(fname, os.X_OK)
  14.  
  15. def _which(exename):
  16.     for path in os.environ['PATH'].split(os.pathsep):
  17.         fname = os.path.join(path, exename)
  18.         if _is_exe(fname):
  19.             return fname
  20.     return None
  21.  
  22. def _ffmpeg_or_avconv():
  23.     return _which('ffmpeg') or _which('avconv') or None
  24.  
  25.  
  26. def _convert_audio(data, ffmpeg_args):
  27.     fd, tmpname = tempfile.mkstemp()
  28.     os.close(fd)
  29.     try:
  30.         exe = _ffmpeg_or_avconv()
  31.         if exe is None:
  32.             raise AudioConversionError('No ffmpeg of avconv installed')
  33.  
  34.         args = [exe, '-i', 'pipe:0', '-y' ]
  35.         args.extend(ffmpeg_args)
  36.         args.append(tmpname)
  37.         ffmpeg = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  38.         ffmpeg.communicate(data)
  39.  
  40.         if ffmpeg.returncode != 0:
  41.             raise AudioConversionError()
  42.  
  43.         with open(tmpname, 'rb') as f:
  44.             return f.read()
  45.     finally:
  46.         os.unlink(tmpname)
  47.  
  48.  
  49. @defer.inlineCallbacks
  50. def _convert_audio_async(data, ffmpeg_args):
  51.     fd, tmpinput = tempfile.mkstemp()
  52.     os.close(fd)
  53.     fd, tmpoutput = tempfile.mkstemp()
  54.     os.close(fd)
  55.     try:
  56.         exe = _ffmpeg_or_avconv()
  57.         if exe is None:
  58.             raise AudioConversionError('No ffmpeg or avconv installed')
  59.  
  60.         with open(tmpinput, 'wb') as f:
  61.             f.write(data)
  62.  
  63.         args = ['-i', tmpinput, '-y'] + ffmpeg_args + [tmpoutput]
  64.         out, err, code = yield getProcessOutputAndValue(exe, args)
  65.  
  66.         if code != 0:
  67.             raise AudioConversionError(err)
  68.  
  69.         with open(tmpoutput, 'rb') as f:
  70.             return f.read()
  71.     finally:
  72.         os.unlink(tmpinput)
  73.         os.unlink(tmpoutput)
  74.  
  75.  
  76. # ADPCM WAV 1ch s16
  77. _adpcm_args = [
  78.     '-f', 'wav',
  79.     '-acodec', 'adpcm_ima_wav',
  80.     '-ac', '1',
  81.     '-ar', '16000',
  82. ]
  83.  
  84. _mp3_args = [
  85.     '-f', 'mp3',
  86.     '-b', '160k',
  87.     '-id3v2_version', '0',
  88. ]
  89.  
  90.  
  91. def convert_to_adpcm(data):
  92.     """ Takes arbitrary audio data and converts it to  """
  93.     return _convert_audio(data, _adpcm_args)
  94.  
  95. def convert_to_mp3(data):
  96.     return _convert_audio(data, _mp3_args)
  97.  
  98. def convert_to_adpcm_async(data):
  99.     return _convert_audio_async(data, _adpcm_args)
  100.  
  101. def convert_to_mp3_async(data):
  102.     return _convert_audio_async(data, _mp3_args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement