Beispiel #1
0
def displayInfo(fileNames):
    """Display information about the ffmpeg libs.
    """
    major, minor, micro = avformat.avformat_version()
    print("avformat: %s.%s.%s \t(cgkit build: %s.%s.%s)" %
          (major, minor, micro, decls.LIBAVFORMAT_VERSION_MAJOR,
           decls.LIBAVFORMAT_VERSION_MINOR, decls.LIBAVFORMAT_VERSION_MICRO))
    major, minor, micro = avcodec.avcodec_version()
    print("avcodec:  %s.%s.%s \t(cgkit build: %s.%s.%s)" %
          (major, minor, micro, decls.LIBAVCODEC_VERSION_MAJOR,
           decls.LIBAVCODEC_VERSION_MINOR, decls.LIBAVCODEC_VERSION_MICRO))
    major, minor, micro = avutil.avutil_version()
    print("avutil:   %s.%s.%s \t(cgkit build: %s.%s.%s)" %
          (major, minor, micro, decls.LIBAVUTIL_VERSION_MAJOR,
           decls.LIBAVUTIL_VERSION_MINOR, decls.LIBAVUTIL_VERSION_MICRO))
    major, minor, micro = swscale.swscale_version()
    print("swscale:  %s.%s.%s \t(cgkit build: %s.%s.%s)" %
          (major, minor, micro, decls.LIBSWSCALE_VERSION_MAJOR,
           decls.LIBSWSCALE_VERSION_MINOR, decls.LIBSWSCALE_VERSION_MICRO))
    print("sizeof(AVFormatContext): %s" % ctypes.sizeof(decls.AVFormatContext))
    print("sizeof(AVCodecContext): %s" % ctypes.sizeof(decls.AVCodecContext))
    print("sizeof(AVPicture): %s" % ctypes.sizeof(decls.AVPicture))
    print("sizeof(SwsContext): %s" % ctypes.sizeof(decls.SwsContext))

    for fileName in fileNames:
        print("\n%s" % fileName)
        avfile = mediafile.open(fileName)
        for stream in avfile.videoStreams:
            res = "%sx%s" % (stream.width, stream.height)
            pixelAspect = stream.pixelAspect
            if pixelAspect is not None and pixelAspect != 1:
                res += " (%sx%s)" % (int(
                    round(float(pixelAspect * stream.width))), stream.height)
            print(
                '  Video Stream: %s, %s, %gfps, %s, %gkbit/s, codec: "%s" (%s)'
                % (duration2str(stream.duration,
                                stream.timeBase), res, stream.frameRate,
                   avutil.av_get_pix_fmt_name(
                       stream._codecCtx.pix_fmt), round(stream.bitRate / 1000),
                   stream.codecLongName, stream.codecName))
        for stream in avfile.audioStreams:
            print(
                '  Audio Stream: %s, %s channels, %sHz, %gkbit/s, codec: "%s" (%s)'
                % (duration2str(stream.duration,
                                stream.timeBase), stream.numChannels,
                   stream.sampleRate, round(stream.bitRate / 1000.0),
                   stream.codecLongName, stream.codecName))


#            print stream._codecCtx.sample_fmt
        avfile.close()
Beispiel #2
0
def playVideo(fileName):
    vid = mediafile.open(fileName)
    print("%s video streams, %s audio streams" %
          (vid.numVideoStreams(), vid.numAudioStreams()))
    if vid.numVideoStreams() == 0:
        print("No video stream found")
        return

    if not pygame_available:
        raise RuntimeError("pygame is not installed")

    pygame.init()

    stream = vid.videoStreams[0]
    print("Video resolution: %sx%s" % (stream.width, stream.height))
    print(stream._codecCtx.time_base.num, stream._codecCtx.time_base.den)

    screen = pygame.display.set_mode((stream.width, stream.height))

    t1 = time.time()
    for i, data in enumerate(vid.iterData()):
        imgArr = data.numpyArray(pixelFormat=mediafile.RGB,
                                 colorAccess=mediafile.SEPARATE_CHANNELS)
        #        img = data.pilImage()
        print("frame %s - shape:%s strides:%s" %
              (i, imgArr.shape, imgArr.strides))
        pygame.surfarray.blit_array(screen, imgArr)

        pygame.display.flip()


#        if i==30:
#            break

    print "time:", time.time() - t1

    exit_flag = False
    while not exit_flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit_flag = True
            elif event.type == pygame.KEYDOWN:
                if event.key == 27:
                    exit_flag = True

    vid.close()
Beispiel #3
0
    def testVideoProperties(self):
        """Test reading a video file and examining its properties.
        """
        # Open the video file
        vid = mediafile.open("data/video1.mp4")

        self.assertEqual(0, vid.numAudioStreams())
        self.assertEqual(1, vid.numVideoStreams())
        stream = vid.videoStreams[0]
        self.assertEqual([], vid.audioStreams)
        self.assertEqual([stream], vid.videoStreams)

        self.assertEqual((320, 240), stream.size)
        self.assertEqual(320, stream.width)
        self.assertEqual(240, stream.height)
        self.assertEqual(fractions.Fraction(25, 1), stream.frameRate)

        vid.close()
Beispiel #4
0
    def testVideoProperties(self):
        """Test reading a video file and examining its properties.
        """
        # Open the video file
        vid = mediafile.open("data/video1.mp4")
        
        self.assertEqual(0, vid.numAudioStreams())
        self.assertEqual(1, vid.numVideoStreams())
        stream = vid.videoStreams[0]
        self.assertEqual([], vid.audioStreams)
        self.assertEqual([stream], vid.videoStreams)
        
        self.assertEqual((320,240), stream.size)
        self.assertEqual(320, stream.width)
        self.assertEqual(240, stream.height)
        self.assertEqual(fractions.Fraction(25,1), stream.frameRate)

        vid.close()
Beispiel #5
0
def playVideo(fileName):
    vid = mediafile.open(fileName)
    print ("%s video streams, %s audio streams" % (vid.numVideoStreams(), vid.numAudioStreams()))
    if vid.numVideoStreams() == 0:
        print ("No video stream found")
        return

    if not pygame_available:
        raise RuntimeError("pygame is not installed")

    pygame.init()

    stream = vid.videoStreams[0]
    print ("Video resolution: %sx%s" % (stream.width, stream.height))
    print (stream._codecCtx.time_base.num, stream._codecCtx.time_base.den)

    screen = pygame.display.set_mode((stream.width, stream.height))

    t1 = time.time()
    for i, data in enumerate(vid.iterData()):
        imgArr = data.numpyArray(pixelFormat=mediafile.RGB, colorAccess=mediafile.SEPARATE_CHANNELS)
        #        img = data.pilImage()
        print ("frame %s - shape:%s strides:%s" % (i, imgArr.shape, imgArr.strides))
        pygame.surfarray.blit_array(screen, imgArr)

        pygame.display.flip()
    #        if i==30:
    #            break

    print "time:", time.time() - t1

    exit_flag = False
    while not exit_flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit_flag = True
            elif event.type == pygame.KEYDOWN:
                if event.key == 27:
                    exit_flag = True

    vid.close()
Beispiel #6
0
def displayInfo(fileNames):
    """Display information about the ffmpeg libs.
    """
    major, minor, micro = avformat.avformat_version()
    print (
        "avformat: %s.%s.%s \t(cgkit build: %s.%s.%s)"
        % (
            major,
            minor,
            micro,
            decls.LIBAVFORMAT_VERSION_MAJOR,
            decls.LIBAVFORMAT_VERSION_MINOR,
            decls.LIBAVFORMAT_VERSION_MICRO,
        )
    )
    major, minor, micro = avcodec.avcodec_version()
    print (
        "avcodec:  %s.%s.%s \t(cgkit build: %s.%s.%s)"
        % (
            major,
            minor,
            micro,
            decls.LIBAVCODEC_VERSION_MAJOR,
            decls.LIBAVCODEC_VERSION_MINOR,
            decls.LIBAVCODEC_VERSION_MICRO,
        )
    )
    major, minor, micro = avutil.avutil_version()
    print (
        "avutil:   %s.%s.%s \t(cgkit build: %s.%s.%s)"
        % (
            major,
            minor,
            micro,
            decls.LIBAVUTIL_VERSION_MAJOR,
            decls.LIBAVUTIL_VERSION_MINOR,
            decls.LIBAVUTIL_VERSION_MICRO,
        )
    )
    major, minor, micro = swscale.swscale_version()
    print (
        "swscale:  %s.%s.%s \t(cgkit build: %s.%s.%s)"
        % (
            major,
            minor,
            micro,
            decls.LIBSWSCALE_VERSION_MAJOR,
            decls.LIBSWSCALE_VERSION_MINOR,
            decls.LIBSWSCALE_VERSION_MICRO,
        )
    )
    print ("sizeof(AVFormatContext): %s" % ctypes.sizeof(decls.AVFormatContext))
    print ("sizeof(AVCodecContext): %s" % ctypes.sizeof(decls.AVCodecContext))
    print ("sizeof(AVPicture): %s" % ctypes.sizeof(decls.AVPicture))
    print ("sizeof(SwsContext): %s" % ctypes.sizeof(decls.SwsContext))

    for fileName in fileNames:
        print ("\n%s" % fileName)
        avfile = mediafile.open(fileName)
        for stream in avfile.videoStreams:
            res = "%sx%s" % (stream.width, stream.height)
            pixelAspect = stream.pixelAspect
            if pixelAspect is not None and pixelAspect != 1:
                res += " (%sx%s)" % (int(round(float(pixelAspect * stream.width))), stream.height)
            print (
                '  Video Stream: %s, %s, %gfps, %s, %gkbit/s, codec: "%s" (%s)'
                % (
                    duration2str(stream.duration, stream.timeBase),
                    res,
                    stream.frameRate,
                    avutil.av_get_pix_fmt_name(stream._codecCtx.pix_fmt),
                    round(stream.bitRate / 1000),
                    stream.codecLongName,
                    stream.codecName,
                )
            )
        for stream in avfile.audioStreams:
            print (
                '  Audio Stream: %s, %s channels, %sHz, %gkbit/s, codec: "%s" (%s)'
                % (
                    duration2str(stream.duration, stream.timeBase),
                    stream.numChannels,
                    stream.sampleRate,
                    round(stream.bitRate / 1000.0),
                    stream.codecLongName,
                    stream.codecName,
                )
            )
        #            print stream._codecCtx.sample_fmt
        avfile.close()
Beispiel #7
0
def playAudio(fileName):
    avfile = mediafile.open(fileName)
    print ("%s video streams, %s audio streams" % (avfile.numVideoStreams(), avfile.numAudioStreams()))
    if avfile.numAudioStreams() == 0:
        print ("No audio stream found")
        return

    if not pygame_available:
        raise RuntimeError("pygame is not installed")

    stream = avfile.audioStreams[0]
    print "%s channels, %sHz" % (stream.numChannels, stream.sampleRate)
    print stream._codecCtx.sample_fmt
    print "Duration", stream.duration

    # SEEK TEST
    #    dstPos = stream.duration-400000000L
    #    dstPos = 0
    #    avformat.av_seek_frame(avfile._formatCtx, 0, ctypes.c_longlong(dstPos), 0)

    pygame.mixer.init(frequency=stream.sampleRate, channels=stream.numChannels)
    pygame.init()

    channel = pygame.mixer.Channel(0)
    channel.set_endevent(pygame.USEREVENT)

    bufSize = 100000
    buf = (bufSize * ctypes.c_short)()

    offset = 0
    exit_flag = False
    for i, data in enumerate(avfile.iterData([stream])):
        #        print i, float(data.pts*stream.timeBase), float(stream.duration*stream.timeBase)
        size = data.sampleSize / 2

        end = offset + size
        remaining = 0
        if end > bufSize:
            remaining = end - bufSize
            end = bufSize
        buf[offset:end] = data.samples[: end - offset]
        offset = end
        if end == bufSize:
            sound = pygame.mixer.Sound(buf)
            if channel.get_queue() is not None:
                # Wait until the queued sound is played...
                while 1:
                    evt = pygame.event.wait()
                    if evt.type == pygame.USEREVENT:
                        break
                    elif evt.type == pygame.KEYDOWN:
                        if evt.key == 27:
                            exit_flag = True
                            break
                while channel.get_queue() is not None:
                    pass
            channel.queue(sound)
            offset = remaining
            buf[0:remaining] = data.samples[size - remaining : size]

        if exit_flag:
            break

    #    sound = pygame.mixer.Sound(buf)
    #    sound2 = pygame.mixer.Sound(buf)
    #    channel.play(sound)
    #    channel.queue(sound2)

    avfile.close()
Beispiel #8
0
    def _testNumpyFrames(self,
                         pixelFormat,
                         pixelAccess,
                         colorAccess=mediafile.SEPARATE_CHANNELS):
        """Test reading video frames into numpy buffers.
        """
        # Open the video file
        vid = mediafile.open("data/video1.mp4")

        # Determine the number of channels in the output buffer...
        if pixelFormat in [mediafile.RGB, mediafile.BGR]:
            numChannels = 3
        elif pixelFormat == mediafile.GRAY:
            numChannels = 1
        else:
            numChannels = 4

        # Initialize the swap flag
        swap = (pixelAccess == mediafile.HEIGHT_WIDTH)

        # Read the frames...
        numFrames = 0
        for i, frame in enumerate(vid.iterData()):
            numFrames += 1

            # Get the numpy array containing the video frame...
            imgArr = frame.numpyArray(pixelFormat=pixelFormat,
                                      pixelAccess=pixelAccess,
                                      colorAccess=colorAccess)

            # Check the shape and dtype of the array...
            if swap:
                shape = (240, 320, numChannels)
            else:
                shape = (320, 240, numChannels)
            dtype = numpy.uint8
            if colorAccess == mediafile.COMBINED_CHANNELS:
                shape = (shape[0], shape[1])
                if numChannels == 4:
                    dtype = numpy.uint32

            self.assertEqual(shape, imgArr.shape)
            self.assertEqual(dtype, imgArr.dtype)

            # Check the red block
            self.assertColor((255, 0, 0),
                             self.readPixel(imgArr, (5, 5), swap, pixelFormat))
            # Check the green block
            self.assertColor((0, 255, 0),
                             self.readPixel(imgArr, (160, 5), swap,
                                            pixelFormat))
            # Check the blue block
            self.assertColor((0, 0, 255),
                             self.readPixel(imgArr, (300, 5), swap,
                                            pixelFormat))
            # Check the cyan block
            self.assertColor((0, 255, 255),
                             self.readPixel(imgArr, (5, 15), swap,
                                            pixelFormat))
            # Check the magenta block
            self.assertColor((255, 0, 255),
                             self.readPixel(imgArr, (160, 15), swap,
                                            pixelFormat))
            # Check the yellow block
            self.assertColor((255, 255, 0),
                             self.readPixel(imgArr, (300, 15), swap,
                                            pixelFormat))
            # Check the white block
            self.assertColor((255, 255, 255),
                             self.readPixel(imgArr, (160, 25), swap,
                                            pixelFormat))

            # Check the moving pixels (to make sure we see the correct frame)
            x = 8 + i * 16
            self.assertColor((255, 0, 0),
                             self.readPixel(imgArr, (x, 40), swap,
                                            pixelFormat))
            self.assertColor((0, 255, 0),
                             self.readPixel(imgArr, (x, 50), swap,
                                            pixelFormat))
            self.assertColor((0, 0, 255),
                             self.readPixel(imgArr, (x, 60), swap,
                                            pixelFormat))
            if i > 0:
                x -= 16
                self.assertColor((0, 0, 0),
                                 self.readPixel(imgArr, (x, 40), swap,
                                                pixelFormat))
                self.assertColor((0, 0, 0),
                                 self.readPixel(imgArr, (x, 50), swap,
                                                pixelFormat))
                self.assertColor((0, 0, 0),
                                 self.readPixel(imgArr, (x, 60), swap,
                                                pixelFormat))

#            img = Image.fromarray(imgData)
#img = frame.pilImage()
#            img.save("frame%02d.png"%i)

# Check that we have seen all 20 frames
        self.assertEqual(20, numFrames)

        vid.close()
Beispiel #9
0
def playAudio(fileName):
    avfile = mediafile.open(fileName)
    print("%s video streams, %s audio streams" %
          (avfile.numVideoStreams(), avfile.numAudioStreams()))
    if avfile.numAudioStreams() == 0:
        print("No audio stream found")
        return

    if not pygame_available:
        raise RuntimeError("pygame is not installed")

    stream = avfile.audioStreams[0]
    print "%s channels, %sHz" % (stream.numChannels, stream.sampleRate)
    print stream._codecCtx.sample_fmt
    print "Duration", stream.duration

    # SEEK TEST
    #    dstPos = stream.duration-400000000L
    #    dstPos = 0
    #    avformat.av_seek_frame(avfile._formatCtx, 0, ctypes.c_longlong(dstPos), 0)

    pygame.mixer.init(frequency=stream.sampleRate, channels=stream.numChannels)
    pygame.init()

    channel = pygame.mixer.Channel(0)
    channel.set_endevent(pygame.USEREVENT)

    bufSize = 100000
    buf = (bufSize * ctypes.c_short)()

    offset = 0
    exit_flag = False
    for i, data in enumerate(avfile.iterData([stream])):
        #        print i, float(data.pts*stream.timeBase), float(stream.duration*stream.timeBase)
        size = data.sampleSize / 2

        end = offset + size
        remaining = 0
        if end > bufSize:
            remaining = end - bufSize
            end = bufSize
        buf[offset:end] = data.samples[:end - offset]
        offset = end
        if end == bufSize:
            sound = pygame.mixer.Sound(buf)
            if channel.get_queue() is not None:
                # Wait until the queued sound is played...
                while 1:
                    evt = pygame.event.wait()
                    if evt.type == pygame.USEREVENT:
                        break
                    elif evt.type == pygame.KEYDOWN:
                        if evt.key == 27:
                            exit_flag = True
                            break
                while channel.get_queue() is not None:
                    pass
            channel.queue(sound)
            offset = remaining
            buf[0:remaining] = data.samples[size - remaining:size]

        if exit_flag:
            break

#    sound = pygame.mixer.Sound(buf)
#    sound2 = pygame.mixer.Sound(buf)
#    channel.play(sound)
#    channel.queue(sound2)

    avfile.close()
Beispiel #10
0
    def _testNumpyFrames(self, pixelFormat, pixelAccess, colorAccess=mediafile.SEPARATE_CHANNELS):
        """Test reading video frames into numpy buffers.
        """
        # Open the video file
        vid = mediafile.open("data/video1.mp4")

        # Determine the number of channels in the output buffer...
        if pixelFormat in [mediafile.RGB, mediafile.BGR]:
            numChannels = 3
        elif pixelFormat==mediafile.GRAY:
            numChannels = 1
        else:
            numChannels = 4
            
        # Initialize the swap flag
        swap = (pixelAccess==mediafile.HEIGHT_WIDTH)

        # Read the frames...
        numFrames = 0        
        for i,frame in enumerate(vid.iterData()):
            numFrames += 1
            
            # Get the numpy array containing the video frame...
            imgArr = frame.numpyArray(pixelFormat=pixelFormat, pixelAccess=pixelAccess, colorAccess=colorAccess)
            
            # Check the shape and dtype of the array...
            if swap:
                shape = (240,320,numChannels)
            else:
                shape = (320,240,numChannels)
            dtype = numpy.uint8
            if colorAccess==mediafile.COMBINED_CHANNELS:
                shape = (shape[0], shape[1])
                if numChannels==4:
                    dtype = numpy.uint32
                
            self.assertEqual(shape, imgArr.shape)
            self.assertEqual(dtype, imgArr.dtype)
            
            # Check the red block           
            self.assertColor((255,0,0), self.readPixel(imgArr, (5,5), swap, pixelFormat))
            # Check the green block
            self.assertColor((0,255,0), self.readPixel(imgArr, (160,5), swap, pixelFormat))
            # Check the blue block
            self.assertColor((0,0,255), self.readPixel(imgArr, (300,5), swap, pixelFormat))
            # Check the cyan block           
            self.assertColor((0,255,255), self.readPixel(imgArr, (5,15), swap, pixelFormat))
            # Check the magenta block
            self.assertColor((255,0,255), self.readPixel(imgArr, (160,15), swap, pixelFormat))
            # Check the yellow block
            self.assertColor((255,255,0), self.readPixel(imgArr, (300,15), swap, pixelFormat))
            # Check the white block
            self.assertColor((255,255,255), self.readPixel(imgArr, (160,25), swap, pixelFormat))
            
            # Check the moving pixels (to make sure we see the correct frame)
            x = 8+i*16
            self.assertColor((255,0,0), self.readPixel(imgArr, (x,40), swap, pixelFormat))
            self.assertColor((0,255,0), self.readPixel(imgArr, (x,50), swap, pixelFormat))
            self.assertColor((0,0,255), self.readPixel(imgArr, (x,60), swap, pixelFormat))
            if i>0:
                x -= 16
                self.assertColor((0,0,0), self.readPixel(imgArr, (x,40), swap, pixelFormat))
                self.assertColor((0,0,0), self.readPixel(imgArr, (x,50), swap, pixelFormat))
                self.assertColor((0,0,0), self.readPixel(imgArr, (x,60), swap, pixelFormat))
            
#            img = Image.fromarray(imgData)
            #img = frame.pilImage()
#            img.save("frame%02d.png"%i)
        
        # Check that we have seen all 20 frames
        self.assertEqual(20, numFrames)
        
        vid.close()