1

我想创建一个脚本,用图像(例如演示文稿的幻灯片)替换视频的某些部分。

我现在的想法是用与演示幻灯片相对应的适当图像替换范围内的每一帧。

我想用 Python 来做,你推荐什么库?

编辑:格式对我来说无关紧要,我总是可以在处理之前进行转换。

4

1 回答 1

1

考虑到您没有指定格式,以下是 AVI 格式的方法:

http://pymedia.org/tut/

从一个文件录制到另一个文件(拆分您需要的帧):

#! /bin/env python
import sys
import pymedia.video.muxer as muxer
import pymedia.video.vcodec as vcodec

def recodeVideo( inFile, outFile, outCodec ):
    dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] )
    f= open( inFile, 'rb' )
    fw= open( outFile, 'wb' )
    s= f.read( 400000 )
    r= dm.parse( s )
    v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams )
    if len( v )== 0:
        raise 'There is no video stream in a file %s' % inFile

    v_id= v[ 0 ][ 'index' ]
    print 'Assume video stream at %d index: ' % v_id
    c= vcodec.Decoder( dm.streams[ v_id ] )
    e= None
    while len( s )> 0:
        for fr in r:
            if fr[ 0 ]== v_id:
                d= c.decode( fr[ 1 ] )
                if e== None and d:
                    params= c.getParams()
                    params[ 'id' ]= vcodec.getCodecID( outCodec )
                    # Just try to achive max quality( 2.7 MB/sec mpeg1 and 9.8 for mpeg2 )
                    if outCodec== 'mpeg1video':
                        params[ 'bitrate' ]= 2700000
                    else:
                        params[ 'bitrate' ]= 9800000
                    # It should be some logic to work with frame rates and such.
                    # I'm not aware of what it would be...
                    print 'Setting codec to ', params
                    e= vcodec.Encoder( params )
                if e and d:
                    dw= e.encode( d )
                    #print 'Frame size ', len( dw )
                    fw.write( dw )

        s= f.read( 400000 )
        r= dm.parse( s )

if __name__== '__main__':
  if len( sys.argv )!= 4:
    print "Usage: recode_video <in_file> <out_file> <format>\n\tformat= { mpeg1video | mpeg2video }"
  else:
    recodeVideo( sys.argv[ 1 ], sys.argv[ 2 ], sys.argv[ 3 ] )

还有其他库,例如 Matroska 文件:

于 2013-05-17T10:41:35.617 回答