ewa.transcode

Package ewa :: Module transcode |
[hide private]
[frames] | no frames]

# Source Code for Module ewa.transcode

     1  import os
     2  import subprocess
     3
     4  from ewa.logutil import debug, warn, exception
     5  from ewa.mp3 import get_vbr_bitrate_samplerate_mode
     6  from ewa.config import Config
     7
     8 -class TranscodeError(RuntimeError): pass
     9
    10 -def lameTranscode(newBitRate,
    11                    newSampleRate,
    12                    newMode,
    13                    masterPath,
    14                    newPath,
    15                    quiet=True):
    16      """ Executes LAME to transcode a file to a new
    17      bit rate, sample rate, and mode. """
    18
    19  ##     # why is this here?
    20  ##     if newMode=='m':
    21  ##         newMode='f'
    22
    23      args = [Config.lame_path,
    24              '--cbr',
    25              '-b',
    26              str(newBitRate),
    27              '-t',
    28              '--resample',
    29              '%0.2f' % (newSampleRate / 1000.0),
    30              '-m',
    31              newMode,
    32              masterPath,
    33              newPath]
    34
    35      if quiet:
    36          args.insert(1, "--quiet")
    37
    38      debug("lame transcode called with args: %s", args)
    39
    40      res = subprocess.call(args)
    41      if res != 0:
    42          raise TranscodeError("LAME transcoder exploded, return code %d" % res)
    43
    44 -def transcode(masterPath,
    45                newPath,
    46                newBitRate,
    47                newSampleRate,
    48                newMode,
    49                allow_master=False,
    50                transcodeFunc=lameTranscode,
    51                **transcodeKwargs):
    52      """
    53      transcodes master file at masterPath to specified bitrate,
    54      samplerate, and mode using transcodeFunc, by default one using
    55      LAME, to perform the transcoding.  Returns either masterPath or
    56      newPath, depending on if the master path or new path should be
    57      used after possible transcoding.
    58      """
    59      # is this an mp3?
    60      if masterPath.endswith('.mp3') or masterPath.endswith('.MP3'):
    61          vbr, bitrate, samplerate, mode = get_vbr_bitrate_samplerate_mode(masterPath)
    62          if vbr:
    63              warn("master mp3 %s is VBR", masterPath)
    64
    65          if (allow_master and bitrate==newBitRate and samplerate==newSampleRate and mode==newMode):
    66              # no need to transcode
    67              return masterPath
    68
    69      dirNewPath = os.path.dirname(newPath)
    70      if not os.path.exists(dirNewPath):
    71          os.makedirs(dirNewPath)
    72
    73      debug('transcoding %s to %s', masterPath, newPath)
    74
    75      transcodeFunc(newBitRate,
    76                    newSampleRate,
    77                    newMode,
    78                    masterPath,
    79                    newPath,
    80                    **transcodeKwargs)
    81      return newPath
    82
    83
    84  __all__ = ['transcode',  'lameTranscode']
    85