예제 #1
0
def transcode(inFile, outFile, tsn=''):

    settings = {}
    settings['video_codec'] = select_videocodec(tsn)
    settings['video_br'] = select_videobr(tsn)
    settings['video_fps'] = select_videofps(inFile, tsn)
    settings['max_video_br'] = select_maxvideobr()
    settings['buff_size'] = select_buffsize()
    settings['aspect_ratio'] = ' '.join(select_aspect(inFile, tsn))
    settings['audio_br'] = select_audiobr(tsn)
    settings['audio_fr'] = select_audiofr(inFile, tsn)
    settings['audio_ch'] = select_audioch(tsn)
    settings['audio_codec'] = select_audiocodec(inFile, tsn)
    settings['ffmpeg_pram'] = select_ffmpegprams(tsn)
    settings['format'] = select_format(tsn)

    cmd_string = config.getFFmpegTemplate(tsn) % settings

    cmd = [ffmpeg_path(), '-i', inFile] + cmd_string.split()
    print 'transcoding to tivo model '+tsn[:3]+' using ffmpeg command:'
    print ' '.join(cmd)
    debug_write(__name__, fn_attr(), ['ffmpeg command is ', ' '.join(cmd)])
    ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    try:
        shutil.copyfileobj(ffmpeg.stdout, outFile)
    except:
        kill(ffmpeg.pid)
예제 #2
0
def transcode(isQuery, inFile, outFile, tsn='', mime='', thead=''):
    settings = {'video_codec': select_videocodec(inFile, tsn, mime),
                'video_br': select_videobr(inFile, tsn),
                'video_fps': select_videofps(inFile, tsn),
                'max_video_br': select_maxvideobr(tsn),
                'buff_size': select_buffsize(tsn),
                'aspect_ratio': ' '.join(select_aspect(inFile, tsn)),
                'audio_br': select_audiobr(tsn),
                'audio_fr': select_audiofr(inFile, tsn),
                'audio_ch': select_audioch(inFile, tsn),
                'audio_codec': select_audiocodec(isQuery, inFile, tsn),
                'audio_lang': select_audiolang(inFile, tsn),
                'ffmpeg_pram': select_ffmpegprams(tsn),
                'ffmpeg_threads': select_ffmpegthreads(),
                'format': select_format(tsn, mime)}

    if isQuery:
        return settings

    ffmpeg_path = config.get_bin('ffmpeg')
    cmd_string = config.getFFmpegTemplate(tsn) % settings
    fname = unicode(inFile, 'utf-8')
    if mswindows:
        fname = fname.encode('iso8859-1')

    if inFile[-5:].lower() == '.tivo':
        tivodecode_path = config.get_bin('tivodecode')
        tivo_mak = config.get_server('tivo_mak')
        tcmd = [tivodecode_path, '-m', tivo_mak, fname]
        tivodecode = subprocess.Popen(tcmd, stdout=subprocess.PIPE,
                                      bufsize=(512 * 1024))
        if tivo_compatible(inFile, tsn)[0]:
            cmd = ''
            ffmpeg = tivodecode
        else:
            cmd = [ffmpeg_path] + select_ffmpegthreads().split() + ['-i', '-'] + cmd_string.split()
            ffmpeg = subprocess.Popen(cmd, stdin=tivodecode.stdout,
                                      stdout=subprocess.PIPE,
                                      bufsize=(512 * 1024))
    else:
        cmd = [ffmpeg_path] + select_ffmpegthreads().split() + ['-i', fname] + cmd_string.split()
        ffmpeg = subprocess.Popen(cmd, bufsize=(512 * 1024),
                                  stdout=subprocess.PIPE)

    if cmd:
        debug('transcoding to tivo model ' + tsn[:3] + ' using ffmpeg command:')
        debug(' '.join(cmd))

    ffmpeg_procs[inFile] = {'process': ffmpeg, 'start': 0, 'end': 0,
                            'last_read': time.time(), 'blocks': []}
    if thead:
        ffmpeg_procs[inFile]['blocks'].append(thead)
    reap_process(inFile)
    return resume_transfer(inFile, outFile, 0)
예제 #3
0
파일: transcode.py 프로젝트: Gimpson/pytivo
def transcode(isQuery, inFile, outFile, tsn=""):
    settings = {
        "video_codec": select_videocodec(inFile, tsn),
        "video_br": select_videobr(inFile, tsn),
        "video_fps": select_videofps(inFile, tsn),
        "max_video_br": select_maxvideobr(tsn),
        "buff_size": select_buffsize(tsn),
        "aspect_ratio": " ".join(select_aspect(inFile, tsn)),
        "audio_br": select_audiobr(tsn),
        "audio_fr": select_audiofr(inFile, tsn),
        "audio_ch": select_audioch(tsn),
        "audio_codec": select_audiocodec(isQuery, inFile, tsn),
        "audio_lang": select_audiolang(inFile, tsn),
        "ffmpeg_pram": select_ffmpegprams(tsn),
        "format": select_format(tsn),
    }

    if isQuery:
        return settings

    ffmpeg_path = config.get_bin("ffmpeg")
    cmd_string = config.getFFmpegTemplate(tsn) % settings
    fname = unicode(inFile, "utf-8")
    if mswindows:
        fname = fname.encode("iso8859-1")

    if inFile[-5:].lower() == ".tivo":
        tivodecode_path = config.get_bin("tivodecode")
        tivo_mak = config.get_server("tivo_mak")
        tcmd = [tivodecode_path, "-m", tivo_mak, fname]
        tivodecode = subprocess.Popen(tcmd, stdout=subprocess.PIPE, bufsize=(512 * 1024))
        if tivo_compatible(inFile, tsn)[0]:
            cmd = ""
            ffmpeg = tivodecode
        else:
            cmd = [ffmpeg_path, "-i", "-"] + cmd_string.split()
            ffmpeg = subprocess.Popen(cmd, stdin=tivodecode.stdout, stdout=subprocess.PIPE, bufsize=(512 * 1024))
    else:
        cmd = [ffmpeg_path, "-i", fname] + cmd_string.split()
        ffmpeg = subprocess.Popen(cmd, bufsize=(512 * 1024), stdout=subprocess.PIPE)

    if cmd:
        debug("transcoding to tivo model " + tsn[:3] + " using ffmpeg command:")
        debug(" ".join(cmd))

    ffmpeg_procs[inFile] = {"process": ffmpeg, "start": 0, "end": 0, "last_read": time.time(), "blocks": []}
    reap_process(inFile)
    return transfer_blocks(inFile, outFile)
예제 #4
0
def mp4_remux(inFile, basename, tsn='', temp_share_path=''):
    outFile = inFile + '.pyTivo-temp'
    newname = basename + '.pyTivo-temp'

    if temp_share_path:
        newname = os.path.splitext(os.path.split(basename)[1])[0] + '.mp4.pyTivo-temp'
        outFile = os.path.join(temp_share_path, newname)

    if os.path.exists(outFile):
        return None  # ugh!

    ffmpeg_path = config.get_bin('ffmpeg')
    fname = unicode(inFile, 'utf-8')
    oname = unicode(outFile, 'utf-8')
    if mswindows:
        fname = fname.encode('iso8859-1')
        oname = oname.encode('iso8859-1')

    settings = {'video_codec': '-vcodec copy',
            'video_br': select_videobr(inFile, tsn),
            'video_fps': select_videofps(inFile, tsn),
            'max_video_br': select_maxvideobr(tsn),
            'buff_size': select_buffsize(tsn),
            'aspect_ratio': ' '.join(select_aspect(inFile, tsn)),
            'audio_br': select_audiobr(tsn),
            'audio_fr': select_audiofr(inFile, tsn),
            'audio_ch': select_audioch(inFile, tsn),
            'audio_codec': select_audiocodec(False, inFile, tsn, 'video/mp4'),
            'audio_lang': select_audiolang(inFile, tsn),
            'ffmpeg_pram': select_ffmpegprams(tsn),
            'format': '-f mp4'}

    cmd_string = config.getFFmpegTemplate(tsn) % settings
    cmd = [ffmpeg_path, '-i', fname] + cmd_string.split() + [oname]

    debug('transcoding to tivo model ' + tsn[:3] + ' using ffmpeg command:')
    debug(' '.join(cmd))

    ffmpeg = subprocess.Popen(cmd)
    debug('remuxing ' + inFile + ' to ' + outFile)
    if ffmpeg.wait():
        debug('error during remuxing')
        os.remove(outFile)
        return None

    return newname
예제 #5
0
def mp4_remux(inFile, basename, tsn=''):
    outFile = inFile + '.pyTivo-temp'
    newname = basename + '.pyTivo-temp'
    if os.path.exists(outFile):
        return None  # ugh!

    ffmpeg_path = config.get_bin('ffmpeg')
    fname = unicode(inFile, 'utf-8')
    oname = unicode(outFile, 'utf-8')
    if mswindows:
        fname = fname.encode('iso8859-1')
        oname = oname.encode('iso8859-1')

    settings = {
        'video_codec': '-vcodec copy',
        'video_br': select_videobr(inFile, tsn),
        'video_fps': select_videofps(inFile, tsn),
        'max_video_br': select_maxvideobr(tsn),
        'buff_size': select_buffsize(tsn),
        'aspect_ratio': ' '.join(select_aspect(inFile, tsn)),
        'audio_br': select_audiobr(tsn),
        'audio_fr': select_audiofr(inFile, tsn),
        'audio_ch': select_audioch(inFile, tsn),
        'audio_codec': select_audiocodec(False, inFile, tsn, 'video/mp4'),
        'audio_lang': select_audiolang(inFile, tsn),
        'ffmpeg_pram': select_ffmpegprams(tsn),
        'format': '-f mp4'
    }

    cmd_string = config.getFFmpegTemplate(tsn) % settings
    cmd = [ffmpeg_path, '-i', fname] + cmd_string.split() + [oname]

    debug('transcoding to tivo model ' + tsn[:3] + ' using ffmpeg command:')
    debug(' '.join(cmd))

    ffmpeg = subprocess.Popen(cmd)
    debug('remuxing ' + inFile + ' to ' + outFile)
    if ffmpeg.wait():
        debug('error during remuxing')
        os.remove(outFile)
        return None

    return newname
예제 #6
0
def transcode(isQuery, inFile, outFile, tsn='', mime='', thead=''):
    settings = {
        'video_codec': select_videocodec(inFile, tsn),
        'video_br': select_videobr(inFile, tsn),
        'video_fps': select_videofps(inFile, tsn),
        'max_video_br': select_maxvideobr(tsn),
        'buff_size': select_buffsize(tsn),
        'aspect_ratio': ' '.join(select_aspect(inFile, tsn)),
        'audio_br': select_audiobr(tsn),
        'audio_fr': select_audiofr(inFile, tsn),
        'audio_ch': select_audioch(inFile, tsn),
        'audio_codec': select_audiocodec(isQuery, inFile, tsn),
        'audio_lang': select_audiolang(inFile, tsn),
        'ffmpeg_pram': select_ffmpegprams(tsn),
        'format': select_format(tsn, mime)
    }

    if isQuery:
        return settings

    ffmpeg_path = config.get_bin('ffmpeg')
    cmd_string = config.getFFmpegTemplate(tsn) % settings
    fname = unicode(inFile, 'utf-8')
    if mswindows:
        fname = fname.encode('iso8859-1')

    if inFile[-5:].lower() == '.tivo':
        tivodecode_path = config.get_bin('tivodecode')
        tivo_mak = config.get_server('tivo_mak')
        tcmd = [tivodecode_path, '-m', tivo_mak, fname]
        tivodecode = subprocess.Popen(tcmd,
                                      stdout=subprocess.PIPE,
                                      bufsize=(512 * 1024))
        if tivo_compatible(inFile, tsn)[0]:
            cmd = ''
            ffmpeg = tivodecode
        else:
            cmd = [ffmpeg_path, '-i', '-'] + cmd_string.split()
            ffmpeg = subprocess.Popen(cmd,
                                      stdin=tivodecode.stdout,
                                      stdout=subprocess.PIPE,
                                      bufsize=(512 * 1024))
    else:
        cmd = [ffmpeg_path, '-i', fname] + cmd_string.split()
        ffmpeg = subprocess.Popen(cmd,
                                  bufsize=(512 * 1024),
                                  stdout=subprocess.PIPE)

    if cmd:
        debug('transcoding to tivo model ' + tsn[:3] +
              ' using ffmpeg command:')
        debug(' '.join(cmd))

    ffmpeg_procs[inFile] = {
        'process': ffmpeg,
        'start': 0,
        'end': 0,
        'last_read': time.time(),
        'blocks': []
    }
    if thead:
        ffmpeg_procs[inFile]['blocks'].append(thead)
    reap_process(inFile)
    return resume_transfer(inFile, outFile, 0)
예제 #7
0
def mp4_remux(inFile, basename, tsn='', temp_share_path=''):
    temp_add = config.get_server('temp_add', '')
    unique_id = ''
    if temp_add:
        unique_id = '_' + config.get_random()
    outFile = inFile  + unique_id + '.pyTivo-temp'
    newname = basename  + unique_id + '.pyTivo-temp'

    if temp_share_path:
        newname = os.path.splitext(os.path.split(basename)[1])[0]  + unique_id + '.mp4.pyTivo-temp'
        outFile = os.path.join(temp_share_path, newname)

    if os.path.exists(outFile):
        debug('File already exists.  Performing full transcode instead')
        return None

    ffmpeg_path = config.get_bin('ffmpeg')
    fname = unicode(inFile, 'utf-8')
    oname = unicode(outFile, 'utf-8')
    if mswindows:
        fname = fname.encode('iso8859-1')
        oname = oname.encode('iso8859-1')

    settings = {'video_codec': '-vcodec copy',
            'video_br': select_videobr(inFile, tsn),
            'video_fps': select_videofps(inFile, tsn),
            'max_video_br': select_maxvideobr(tsn),
            'buff_size': select_buffsize(tsn),
            'aspect_ratio': ' '.join(select_aspect(inFile, tsn)),
            'audio_br': select_audiobr(tsn),
            'audio_fr': select_audiofr(inFile, tsn),
            'audio_ch': select_audioch(inFile, tsn),
            'audio_codec': select_audiocodec(False, inFile, tsn, 'video/mp4'),
            'audio_lang': select_audiolang(inFile, tsn),
            'ffmpeg_pram': select_ffmpegprams(tsn),
            'ffmpeg_threads': select_ffmpegthreads(),
            'format': '-f mp4'}

    cmd_string = config.getFFmpegTemplate(tsn) % settings

    cmd = [ffmpeg_path] + select_ffmpegthreads().split() + ['-i', fname] + cmd_string.split() + [oname]

    debug('transcoding to tivo model ' + tsn[:3] + ' using ffmpeg command:')
    debug(' '.join(cmd))

    ffmpeg = subprocess.Popen(cmd)

    debug('remuxing ' + inFile + ' to ' + outFile)

    # attempt to overcome certain FFmpeg audio timestamp errors
    # by forcing the audio stream to be transcoded
    # works more reliably with versions of FFmpeg >= 0.11.x
    # large amounts of warnings during mux are expected

    # also bypasses ac3 stream copy regression in FFmpeg
    # error: 'codec frame size is not set'
    # 02/20/2012 - lavc ver >= 54.x.x
    # commit 16e54ac7255d47e70ba9ba60d5ce5d0a0e44b223
    if ffmpeg.wait() == 1 and 'acodec copy' in settings['audio_codec']:
        debug('FFmpeg error, attempting to transcode audio as workaround')

        settings['audio_codec'] = '-acodec ac3' # don't use -copyts
        cmd_string = config.getFFmpegTemplate(tsn) % settings
        cmd = [ffmpeg_path] + select_ffmpegthreads().split() + ['-y', '-i', fname] + cmd_string.split() + [oname]
        
        debug('transcoding to tivo model ' + tsn[:3] + ' using ffmpeg command:')
        debug(' '.join(cmd))

        ffmpeg = subprocess.Popen(cmd)

    if ffmpeg.wait():
        try:
        	os.remove(outFile)
        	debug('FFmpeg error, temp file has been removed: ' + outFile)
        except:
            logger.error('FFmpeg returned a fatal error, ' +
                         'check debug log and configuration settings')
            pass
        return None

    return newname