我想创建一个脚本,用图像替换视频的某些部分(例如,使用演示文稿的幻灯片)。
我现在的想法是用对应于演示幻灯片的适当图像替换范围中的每个帧。
我想用Python做,你会推荐哪些库?
编辑:格式对我来说无关紧要,我可以在处理前随时转换。
答案 0 :(得分:1)
考虑到您没有指定格式,以下是AVI格式的方法:
从一个文件录制到另一个文件(拆分所需的帧):
#! /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文件: