def setUp(self): # Initialize ffmpeg once if not self._avInitialized: avformat.av_register_all() self._avInitialized = True
def readPackets(fileName): """Read the packets of the given file and print them. Uses the ffmpeg module directly. """ avformat.av_register_all() # Open the file formatCtx = avformat.av_open_input_file(fileName, None, 0, None) # Fill the 'streams' fields... avformat.av_find_stream_info(formatCtx) # Get the AVInputFormat object inputFormat = formatCtx.iformat.contents print("Format: %s (%s)" % (inputFormat.long_name, inputFormat.name)) print("Duration: %s min" % (formatCtx.duration / decls.AV_TIME_BASE / 60)) # Iterate over the streams videoIdx = None for i in range(formatCtx.nb_streams): stream = formatCtx.streams[i].contents codecCtx = stream.codec.contents v = codecCtx.codec_tag fourCC = "%s%s%s%s" % (chr(v & 0xff), chr((v >> 8) & 0xff), chr((v >> 16) & 0xff), chr((v >> 24) & 0xff)) if codecCtx.codec_type == decls.CODEC_TYPE_VIDEO: videoIdx = i print("Stream %s: Video '%s', %sx%s" % (i, fourCC, codecCtx.width, codecCtx.height)) elif codecCtx.codec_type == decls.CODEC_TYPE_AUDIO: print("Stream %s: Audio '%s'" % (i, fourCC)) else: print("Stream %s: Other" % i) if videoIdx is not None: stream = formatCtx.streams[videoIdx].contents codecCtx = stream.codec.contents codec = avcodec.avcodec_find_decoder(codecCtx.codec_id) if codec is None: raise RuntimeError("Could not find decoder") print("Video Codec: %s" % codec.long_name) # Open the codec avcodec.avcodec_open(codecCtx, codec) avcodec.avcodec_close(codecCtx) # Read the packets... pkt = decls.AVPacket() eof = False idx = 0 while not eof: # Read the next frame packet... try: eof = not avformat.av_read_frame(formatCtx, pkt) if not eof: print( "Packet %s, stream_idx:%s, pts:%s, dts:%s, size:%s, duration:%s" % (idx, pkt.stream_index, pkt.pts, pkt.dts, pkt.size, pkt.duration)) idx += 1 finally: avcodec.av_free_packet(pkt) avformat.av_close_input_file(formatCtx)
def readPackets(fileName): """Read the packets of the given file and print them. Uses the ffmpeg module directly. """ avformat.av_register_all() # Open the file formatCtx = avformat.av_open_input_file(fileName, None, 0, None) # Fill the 'streams' fields... avformat.av_find_stream_info(formatCtx) # Get the AVInputFormat object inputFormat = formatCtx.iformat.contents print ("Format: %s (%s)" % (inputFormat.long_name, inputFormat.name)) print ("Duration: %s min" % (formatCtx.duration / decls.AV_TIME_BASE / 60)) # Iterate over the streams videoIdx = None for i in range(formatCtx.nb_streams): stream = formatCtx.streams[i].contents codecCtx = stream.codec.contents v = codecCtx.codec_tag fourCC = "%s%s%s%s" % (chr(v & 0xFF), chr((v >> 8) & 0xFF), chr((v >> 16) & 0xFF), chr((v >> 24) & 0xFF)) if codecCtx.codec_type == decls.CODEC_TYPE_VIDEO: videoIdx = i print ("Stream %s: Video '%s', %sx%s" % (i, fourCC, codecCtx.width, codecCtx.height)) elif codecCtx.codec_type == decls.CODEC_TYPE_AUDIO: print ("Stream %s: Audio '%s'" % (i, fourCC)) else: print ("Stream %s: Other" % i) if videoIdx is not None: stream = formatCtx.streams[videoIdx].contents codecCtx = stream.codec.contents codec = avcodec.avcodec_find_decoder(codecCtx.codec_id) if codec is None: raise RuntimeError("Could not find decoder") print ("Video Codec: %s" % codec.long_name) # Open the codec avcodec.avcodec_open(codecCtx, codec) avcodec.avcodec_close(codecCtx) # Read the packets... pkt = decls.AVPacket() eof = False idx = 0 while not eof: # Read the next frame packet... try: eof = not avformat.av_read_frame(formatCtx, pkt) if not eof: print ( "Packet %s, stream_idx:%s, pts:%s, dts:%s, size:%s, duration:%s" % (idx, pkt.stream_index, pkt.pts, pkt.dts, pkt.size, pkt.duration) ) idx += 1 finally: avcodec.av_free_packet(pkt) avformat.av_close_input_file(formatCtx)