【问题标题】:Replace frame range with image in a video with python用python视频中的图像替换帧范围
【发布时间】:2013-05-17 10:38:18
【问题描述】:

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

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

我想用 Python 来做,你会推荐哪些库?

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

【问题讨论】:

  • 什么视频格式? .mkv、.avi、.mp4...等等。这很重要。
  • 没关系 - avi 没问题,我之前总是可以转换。
  • 好的,我在下面添加了一个简短的 AVI 示例,如何读取和写入 AVI 数据。请注意,为了将来参考,指定它确实很重要,因为如果您后来发现您想要一个 AVI 解决方案,这里有人会否决答案:)
  • 感谢您的回答,但它并没有真正展示如何将图像放入框架中。我也知道 pymedia 教程。

标签: python video video-processing


【解决方案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 文件:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 2011-08-17
    • 2010-10-24
    • 2018-07-17
    • 1970-01-01
    • 2016-01-21
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多