Exemplo n.º 1
0
    def testAvFormat(self):
        """avformat test: Reading a video file.
        """

        #        print ("AvFormat lib version: %s"%(avformat.avformat_version(),))
        #        print (decls.LIBAVFORMAT_VERSION_MAJOR, decls.LIBAVFORMAT_VERSION_MINOR, decls.LIBAVFORMAT_VERSION_MICRO)

        # Open the video file
        formatCtx = avformat.av_open_input_file("data/video1.mp4", None, 0,
                                                None)

        self.assertEqual(decls.AVFormatContext, type(formatCtx))
        self.assertEqual("data/video1.mp4", formatCtx.filename)
        self.assertEqual(1, formatCtx.nb_streams)

        # Fill the 'streams' fields...
        avformat.av_find_stream_info(formatCtx)

        self.assertEqual(
            0.8,
            float(formatCtx.duration) /
            decls.AV_TIME_BASE)  # 20 frames with a framerate of 25 -> 0.8s
        self.assertEqual(4787, formatCtx.file_size)

        # Get the video stream (there is only one stream)
        stream = formatCtx.streams[0].contents
        self.assertEqual(decls.AVStream, type(stream))
        self.assertEqual(20, stream.nb_frames)  # 20 frames
        self.assertEqual(1, stream.time_base.num)  # 1/25 seconds per time unit
        self.assertEqual(25, stream.time_base.den)
        self.assertEqual(20, stream.duration)  # 20 time units (frames)
        self.assertEqual(0, stream.start_time)
        self.assertEqual(25, stream.r_frame_rate.num)  # 25 frames per second
        self.assertEqual(1, stream.r_frame_rate.den)

        # Get the video codec
        codec = stream.codec.contents
        self.assertEqual(decls.AVCodecContext, type(codec))
        self.assertEqual(decls.CODEC_TYPE_VIDEO, codec.codec_type)
        self.assertEqual(0x31637661, codec.codec_tag)  # "fvc1"
        self.assertEqual(320, codec.width)
        self.assertEqual(240, codec.height)
        self.assertEqual(decls.PIX_FMT_YUV420P, codec.pix_fmt)
        self.assertEqual(37740, codec.bit_rate)  # 37kb/s

        # Close the file again
        avformat.av_close_input_file(formatCtx)
Exemplo n.º 2
0
    def testAvFormat(self):
        """avformat test: Reading a video file.
        """

#        print ("AvFormat lib version: %s"%(avformat.avformat_version(),))
#        print (decls.LIBAVFORMAT_VERSION_MAJOR, decls.LIBAVFORMAT_VERSION_MINOR, decls.LIBAVFORMAT_VERSION_MICRO)
        
        # Open the video file
        formatCtx = avformat.av_open_input_file("data/video1.mp4", None, 0, None)
        
        self.assertEqual(decls.AVFormatContext, type(formatCtx))
        self.assertEqual("data/video1.mp4", formatCtx.filename)
        self.assertEqual(1, formatCtx.nb_streams)
        
        # Fill the 'streams' fields...
        avformat.av_find_stream_info(formatCtx)

        self.assertEqual(0.8, float(formatCtx.duration)/decls.AV_TIME_BASE)  # 20 frames with a framerate of 25 -> 0.8s
        self.assertEqual(4787, formatCtx.file_size)
        
        # Get the video stream (there is only one stream)
        stream = formatCtx.streams[0].contents
        self.assertEqual(decls.AVStream, type(stream))
        self.assertEqual(20, stream.nb_frames)         # 20 frames
        self.assertEqual(1, stream.time_base.num)      # 1/25 seconds per time unit
        self.assertEqual(25, stream.time_base.den)
        self.assertEqual(20, stream.duration)          # 20 time units (frames)
        self.assertEqual(0, stream.start_time)
        self.assertEqual(25, stream.r_frame_rate.num)  # 25 frames per second
        self.assertEqual(1, stream.r_frame_rate.den)

        # Get the video codec        
        codec = stream.codec.contents
        self.assertEqual(decls.AVCodecContext, type(codec))
        self.assertEqual(decls.CODEC_TYPE_VIDEO, codec.codec_type)
        self.assertEqual(0x31637661, codec.codec_tag)  # "fvc1"
        self.assertEqual(320, codec.width)
        self.assertEqual(240, codec.height)
        self.assertEqual(decls.PIX_FMT_YUV420P, codec.pix_fmt)
        self.assertEqual(37740, codec.bit_rate)  # 37kb/s

        # Close the file again
        avformat.av_close_input_file(formatCtx)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)