Exemplo n.º 1
0
    def initVideo(self):
        dm = muxer.Demuxer(self.format)

        #try to get multiple streams of a multiplexed file
        junk = dm.parse(self.rawData[30000])
        if len(dm.streams) == 0:
            #set bitrate
            if self.codec == 'mpeg1video':
                bitrate = 2700000
            else:
                bitrate = 9800000
            self.params= { \
                'type': 0,
                'gop_size': 12,
                'frame_rate_base': 125,
                'max_b_frames': 0,
                'width': 800,
                'height': 600,
                'frame_rate': 3125,
                'deinterlace': 0,
                'bitrate': bitrate,
                'id': vcodec.getCodecID( self.codec )
                }
        elif len(dm.streams) == 1:
            self.params = dm.streams[0]
        else:
            #loop through to find the first video stream in the file
            for vindex in xrange(len(dm.streams)):
                if dm.streams[vindex]['type'] == muxer.CODEC_TYPE_VIDEO:
                    self.params = dm.streams[vindex]
                    break

        print self.params

        try:
            # Set the initial sound delay to 0 for now

            # It defines initial offset from video in the beginning of the stream

            self.initADelta = -1
            self.seekADelta = 0
            # Setting up the HW video codec

            self.vc = ext_codecs.Decoder(self.params)
        except:
            self.vc = vcodec.Decoder(self.params)
            try:
                # Fall back to SW video codec

                self.vc = vcodec.Decoder(self.params)
            except:
                traceback.print_exc()
                self.err.append(sys.exc_info()[1])
Exemplo n.º 2
0
 def _getStreamParams( self, vindex, aindex, vparams, aparams, r ):
   # Decode one video frame and 1 audio frame to get stream data
   vDec= aDec= None
   for d in r:
     try:
       # Demux file into streams
       if d[ 0 ]== vindex:
         if not vDec:
           vDec= vcodec.Decoder( vparams )
         vfr= vDec.decode( d[ 1 ] )
         if vfr and vfr.data:
           if vfr.aspect_ratio> .0:
             self.pictureSize= ( int( vfr.size[ 1 ]* vfr.aspect_ratio ), vfr.size[ 1 ] )
           else:
             self.pictureSize= vfr.size
           
           self.vBitRate= vfr.bitrate
           if aindex== -1 or aDec:
             break
       elif d[ 0 ]== aindex:
         if not aDec:
           aDec= acodec.Decoder( aparams )
         afr= aDec.decode( d[ 1 ] )
         if afr and afr.data:
           self.aBitRate= afr.bitrate
           self.aSampleRate= afr.sample_rate
           self.aChannels= afr.channels
           if vindex== -1 or vDec:
             break
     except:
       self.err.append( sys.exc_info() )
       break
Exemplo n.º 3
0
def videoDecodeBenchmark( inFile, opt ):
  dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] )
  f= open( inFile, 'rb' )
  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
  
  a= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_AUDIO, dm.streams )
  if len( a )== 0:
    print 'There is no audio stream in a file %s. Ignoring audio.' % inFile
    opt= 'noaudio'
  else:
    a_id= a[ 0 ][ 'index' ]
  
  t= time.time()
  
  vc= vcodec.Decoder( dm.streams[ v_id ] )
  print dm.streams[ v_id ]
  if opt!= 'noaudio':
    import pymedia.audio.acodec as acodec
    import pymedia.audio.sound as sound
    ac= acodec.Decoder( dm.streams[ a_id ] )
  resampler= None
  frames= 0
  while len( s )> 0:
    for fr in r:
      if fr[ 0 ]== v_id:
        d= vc.decode( fr[ 1 ] )
        if fr[ 3 ]> 0:
          print 'Video PTS', fr[ 3 ]
        if d and d.data:
          frames+= 1
          #ff= open( 'c:\\test', 'wb' )
          #ff.write( d.data[ 0 ] )
          #ff.close()
      elif opt!= 'noaudio' and fr[ 0 ]== a_id:
        d= ac.decode( fr[ 1 ] )
        if resampler== None:
          if d and d.channels> 2:
            resampler= sound.Resampler( (d.sample_rate,d.channels), (d.sample_rate,2) )
        else:
          data= resampler.resample( d.data )

    s= f.read( 400000 )
    r= dm.parse( s )
    
    tt= time.time()- t
    print '%d frames in %d secs( %.02f fps )' % ( frames, tt, float(frames)/tt )
Exemplo n.º 4
0
 def _initVideo( self, params ):
   # There is no overlay created yet
   try:
     # Set the initial sound delay to 0 for now
     # It defines initial offset from video in the beginning of the stream
     self.initADelta= -1
     self.vindex= -1
     self._resetVideo()
     self.seekADelta= 0
     # Setting up the HW video codec
     self.vc= pymedia.video.ext_codecs.Decoder( params ) 
   except:
     try:
       # Fall back to SW video codec
       self.vc= vcodec.Decoder( params )
     except:
       pass
Exemplo n.º 5
0
 def initVideo(self, params):
     # There is no overlay created yet
     self.overlay = self.pictureSize = None
     try:
         # Set the initial sound delay to 0 for now
         # It defines initial offset from video in the beginning of the stream
         self.initADelta = -1
         self.resetVideo()
         self.seekADelta = 0
         # Setting up the HW video codec
         self.vc = pymedia.video.ext_codecs.Decoder(params)
     except:
         try:
             # Fall back to SW video codec
             self.vc = vcodec.Decoder(params)
         except:
             traceback.print_exc()
             self.err.append(sys.exc_info()[1])
Exemplo n.º 6
0
def dumpVideo(inFile, outFilePattern, fmt):
    dm = muxer.Demuxer(inFile.split('.')[-1])
    i = 1
    f = open(inFile, 'rb')
    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, dm.streams[v_id]
    c = vcodec.Decoder(dm.streams[v_id])
    print 'Decoder created'
    t = time.time()
    while len(s) > 0:
        for fr in r:
            if (i % 100) == 0:
                print '%d frames in %.2f secs (%.2f fps)' % (i, time.time() -
                                                             t, i /
                                                             (time.time() - t))
            if fr[0] == v_id:
                d = c.decode(fr[1])
                # Save file as RGB BMP or YUV
                if d and d.data:
                    if fmt == 2:
                        dd = d.convert(fmt)
                        img = pygame.image.fromstring(dd.data, dd.size, "RGB")
                        pygame.image.save(img, outFilePattern % i)
                    elif fmt == 0:
                        f = open(outFilePattern % i, 'wb')
                        f.write(d.data[0])
                        f.write(d.data[1])
                        f.write(d.data[2])
                        f.close()

                    i += 1

        s = f.read(400000)
        r = dm.parse(s)

    print 'Saved %d frames' % (i - 1)
Exemplo n.º 7
0
 def _getVStreamParams( self, vindex, vparams, r ):
   # Decode one video frame and 1 audio frame to get stream data
   vDec= None
   for d in r:
     try:
       # Demux file into streams
       if d[ 0 ]== vindex:
         if not vDec:
           vDec= vcodec.Decoder( vparams )
         vfr= vDec.decode( d[ 1 ] )
         if vfr and vfr.data:
           if vfr.aspect_ratio> .0:
             self.pictureSize= ( int( vfr.size[ 1 ]* vfr.aspect_ratio ), vfr.size[ 1 ] )
           else:
             self.pictureSize= vfr.size
           
           self.vBitRate= vfr.bitrate
           break
     except:
       self.err.append( sys.exc_info() )
       break
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 )
Exemplo n.º 9
0
def videoDecodeBenchmark(inFile, opt):
    pygame.init()
    pygame.display.set_mode((800, 600), 0)
    ovl = None

    dm = muxer.Demuxer(inFile.split('.')[-1])
    f = open(inFile, 'rb')
    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

    a = filter(lambda x: x['type'] == muxer.CODEC_TYPE_AUDIO, dm.streams)
    if len(a) == 0:
        print 'There is no audio stream in a file %s. Ignoring audio.' % inFile
        opt = 'noaudio'
    else:
        a_id = a[0]['index']

    t = time.time()

    vc = vcodec.Decoder(dm.streams[v_id])
    print dm.streams[v_id]
    if opt != 'noaudio':
        ac = acodec.Decoder(dm.streams[a_id])
    resampler = None
    frames = 0
    q = []
    while len(s) > 0:
        for fr in r:
            if fr[0] == v_id:
                d = vc.decode(fr[1])
                if d and d.data:
                    frames += 1
                    #ff= open( 'c:\\test', 'wb' )
                    #ff.write( d.data[ 0 ] )
                    #ff.close()
                    if not ovl:
                        ovl = pygame.Overlay(YV12, d.size)
                    q.append(d)
                    if len(q) > 4:
                        try:
                            ovl.set_data(q[0].data)
                            ovl.display()
                        except:
                            ovl.display(q[0].data)

                        del (q[0])
            elif opt != 'noaudio' and fr[0] == a_id:
                d = ac.decode(fr[1])
                if resampler == None:
                    if d and d.channels > 2:
                        resampler = sound.Resampler(
                            (d.sample_rate, d.channels), (d.sample_rate, 2))
                else:
                    data = resampler.resample(d.data)

        s = f.read(400000)
        r = dm.parse(s)

        tt = time.time() - t
        print '%d frames in %d secs( %.02f fps )' % (frames, tt,
                                                     float(frames) / tt)

        ev = pygame.event.get()
        for e in ev:
            if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
                s = ''
                break
Exemplo n.º 10
0
def read_frames_from_video(avi_file,
                           image_size,
                           color_channels=3,
                           count_only=False,
                           read_range=None):
    dm = muxer.Demuxer(avi_file.split('.')[-1])
    f = open(avi_file, 'rb')
    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 Exception('There is no video stream in a file %s' % inFile)
    v_id = v[0]['index']
    c = vcodec.Decoder(dm.streams[v_id])
    frame_count = 0

    if count_only:
        frames = None
    elif read_range == None:
        frames = np.zeros((1, color_channels, image_size[1], image_size[0]),
                          dtype=theano.config.floatX)
    else:
        time_depth = read_range[1] - read_range[0] + 1
        frames = np.zeros(
            (time_depth, color_channels, image_size[1], image_size[0]),
            dtype=theano.config.floatX)
        print(frames.shape)

    have_read = False
    finished = False
    actual_frames_read = 0
    while len(s) > 0 and not finished:
        for fr in r:
            if fr[0] == v_id:
                d = c.decode(fr[1])
                # Save file as RGB BMP
                if d:
                    if count_only:
                        frame_count += 1
                        actual_frames_read += 1
                        continue
                    if not read_range == None and frame_count < read_range[0]:
                        frame_count = frame_count + 1
                        continue
                    elif not read_range == None and frame_count > read_range[1]:
                        finished = True
                        break

                    dd = d.convert(2)

                    if color_channels == 3:
                        img = Image.frombytes("RGB", dd.size,
                                              dd.data).resize(image_size)
                    elif color_channels == 1:
                        img = Image.frombytes(
                            "RGB", dd.size,
                            dd.data).convert('L').resize(image_size)
                    else:
                        raise Exception("Unknown color_channels value")

                    img = np.asarray(img, dtype=dtype_to_use) / 256.
                    if (color_channels == 1): img = np.expand_dims(img, 2)
                    img = img.transpose(
                        2, 0, 1
                    )  #.reshape(1, color_channels, image_size[1], image_size[0])
                    if not have_read:
                        frames[0, :, :, :] = img
                        have_read = True
                    elif read_range == None:
                        frames = np.concatenate(
                            (frames, np.expand_dims(img, 0)), 0)
                    else:
                        frames[actual_frames_read, :, :, :] = img

                    frame_count += 1
                    actual_frames_read += 1

        if not finished:
            s = f.read(400000)
            r = dm.parse(s)

    return (actual_frames_read, frames)
Exemplo n.º 11
0
def recodeVideo( inFile, outFile, outCodec ):
	dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] )
	f= open( inFile, 'rb' )
	open( outFile, 'wb' ).close()
	s= f.read( 600000 )
	r= dm.parse( s )
	v= [ x for x in dm.streams if x[ 'type' ]== muxer.CODEC_TYPE_VIDEO ]
	if len( v )== 0:
		raise 'There is no video stream in a file %s' % inFile
	
	v_id= v[ 0 ][ 'index' ]
	print 'Video stream at %d index: ' % v_id
	
	a_id= -1
	a= [ x for x in dm.streams if x[ 'type' ]== muxer.CODEC_TYPE_AUDIO ]
	if len( a )!= 0:
		a_id= a[ 0 ][ 'index' ]
		print 'Assume audio stream at %d index: ' % a_id
	
	vc= vcodec.Decoder( v[ 0 ] )
	if a_id!= -1:
		ac= acodec.Decoder( a[ 0 ] )
	ea= ev= None
	bA= bV= 0
	# Create muxer first
	mx= muxer.Muxer( outFile.split( '.' )[ -1 ] )
	for fr in r:
		if fr[ 0 ]== a_id and not bA:
			d= ac.decode( fr[ 1 ] )
			if d and d.data:
				# --------- Just a test !
				params= ac.getParams()
				# --------- End of test
				
				#params= { 'id': acodec.getCodecID('ac3'),
				#		'bitrate': d.bitrate,
				#		'sample_rate': d.sample_rate,
				#		'channels': d.channels }
				
				# It should be some logic to work with frame rates and such.
				# I'm not aware of what it would be...
				print 'Setting audio codec to ', params
				#ea= acodec.Encoder( params )
				aId= mx.addStream( muxer.CODEC_TYPE_AUDIO, params )
				bA= 1
		
		if fr[ 0 ]== v_id and not bV:
			d= vc.decode( fr[ 1 ] )
			if d and d.data:
				# --------- Just a test !
				params= vc.getParams()
				# --------- End of test
				
				#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 video codec to ', params
				#ev= vcodec.Encoder( params )
				vId= mx.addStream( muxer.CODEC_TYPE_VIDEO, params )
				bV= 1
	
	# Reset decoders
	#if a_id!= -1:
	#	ac= acodec.Decoder( a[ 0 ] )
	#vc= vcodec.Decoder( v[ 0 ] )
	
	# Start muxer			
	fw= open( outFile, 'ab' )
	fw.write( mx.start() )				
	fw.close()
	
	while len( s )> 0:
		for fr in r:
			ss= None
			if fr[ 3 ]> 0:
				#print 'PTS', fr[ 0 ], fr[ 3 ], fr[ 4 ]
				pass
				#if fr[ 3 ]> 3000000:
				#	return
			
			if fr[ 0 ]== a_id:
				# --------- Just a test !
				ss= mx.write( aId, fr[ 1 ], fr[ 3 ], fr[ 4 ] )
				#print len(fr[ 1 ]), fr[ 3 ]
				# --------- End of test
				#d= ac.decode( fr[ 1 ] )
				#if d and d.data:
				#	enc_frames= ea.encode( d.data )
				#	if enc_frames:
				#		for efr in enc_frames:
				#			ss= mx.write( aId, efr )
			
			if fr[ 0 ]== v_id:
				# --------- Just a test !
				ss= mx.write( vId, fr[ 1 ], fr[ 3 ], fr[ 4 ] )
				#if ss: fw.write(ss)
				#continue
				# --------- End of test
				#d= vc.decode( fr[ 1 ] )
				#if d and d.data:
				#	efr= ev.encode( d )
				#	if efr and efr.data:
				#		ss= mx.write( vId, efr.data )
			
			if ss:
				fw= open( outFile, 'ab' )
				fw.write( ss )				
				fw.close()
		
		
		s= f.read( 4000 )
		r= dm.parse( s )
	
	if fw and mx:
		ss= mx.end()
		if ss: 
			fw= open( outFile, 'ab' )
			fw.write( ss )				
			fw.close()
Exemplo n.º 12
0
    def vid(self, queue):
        frRate = 0
        vc = surfaces = None
        sndDelay = lastPts = pts = vPTS = hurry = 0
        clock = 0
        delay = .0
        print 'VPlayer: Video thread has started'
        try:
            while 1:
                # Video stream processing
                d = queue.get()
                if type(d) == str:
                    if d == 'SEEK':
                        hurry = 0
                        if vc: vc.reset()
                        sndDelay = -.5
                    if d == 'STOP':
                        break
                    if d == 'START':
                        self.overlay = None
                        params = queue.get()
                        try:
                            #print 'External codec params are:', params
                            vc = pymedia.video.ext_codecs.Decoder(params)
                            sndDelay = 0
                        except:
                            print 'Falling back to software decoder'
                            try:
                                vc = vcodec.Decoder(params)
                                sndDelay = 0
                            except:
                                traceback.print_exc()
                                self.err.append(sys.exc_info()[1])
                                continue

                        clock = time.time()

                    continue

                if self.playingFile == None or len(self.err) > 0 or self.seek:
                    continue

                if vc == None:
                    continue

                try:
                    vfr = vc.decode(d[1], d[3])
                    #if hurry== 0:
                    #  time.sleep( .035 )
                except:
                    cls = sys.exc_info()[0].__name__
                    if cls.find('NeedBuffer') != -1:
                        params = vc.getParams()
                        #print 'External codec need buffers', params
                        size = params['width'], params['height']
                        surfaces = [
                            pygame.Surface(size, 0, YV12) for x in range(4)
                        ]
                        st = [x.lock() for x in surfaces]
                        offs = [x.get_offset() for x in surfaces]
                        #print 'Surfaces allocated', offs
                        vc.setBuffers(st[0][1], offs)
                        vfr = vc.decode(d[1], d[3])
                    else:
                        raise

                if vfr and vfr.data:
                    if self.skipSound:
                        self.frameNum = 0
                        self.skipSound = 0
                        vPTS = 0

                    if self.vBitRate == 0: self.vBitRate = vfr.bitrate

                    if vfr.pict_type == 1 or vfr.pict_type == 2:
                        if self.frameNum > 0 and vfr.frame_pts > 0:
                            vPTS = vfr.frame_pts
                        else:
                            vPTS = lastPts + 90000 * vfr.rate_base / vfr.rate
                    else:
                        vPTS = lastPts + 90000 * vfr.rate_base / vfr.rate
                    lastPts = vPTS
                    vPTS = float(vPTS) / 90000
                    print 'VPTS =', lastPts, vPTS * 90000, vfr.frame_pts, 'FRAME=', vfr.pict_type
                    #print d[3]
                    #if d[ 3 ]> 0:
                    # Calc frRate
                    #if lastPts< d[3] and self.frameNum:
                    #lastPts= d[3]
                    #vPTS= float( d[ 3 ] ) / 90000
                    #frRate= vPTS / self.frameNum
                    #if frRate> .04:
                    #frRate= .04

                    #sndDelay= -.5
                    if self.overlayLoc and self.overlay == None:
                        self.overlay = pygame.Overlay(YV12, vfr.size)
                        if vfr.aspect_ratio > .0:
                            self.pictureSize = (vfr.size[1] * vfr.aspect_ratio,
                                                vfr.size[1])
                        else:
                            self.pictureSize = vfr.size
                        self.setOverlay(self.overlayLoc)

                    if self.frameNum > 0:
                        # use audio clock
                        aPTS = self.getAPTS()
                        if aPTS == 0:
                            # use our internal clock instead
                            aPTS = time.time() - clock

                        delay = vPTS - aPTS + sndDelay  # mpeg2 +.3, avi -.2
                        #print 'Delay', delay, self.frameNum, vPTS, aPTS, frRate
                        if delay < 0:
                            hurry = -delay / frRate
                        else:
                            hurry = 0
                        if delay > 0 and delay < 2:
                            time.sleep(delay)
                    else:
                        frRate = float(vfr.rate_base) / float(vfr.rate)

                    if self.overlay and vfr.data:
                        if surfaces:
                            self.overlay.surface().blit(
                                surfaces[vfr.index], (0, 0))
                            self.overlay.display()
                        else:
                            try:
                                self.overlay.set_data(vfr.data)
                                self.overlay.display()
                            except:
                                self.overlay.display(vfr.data)

                    self.frameNum += 1
                    vPTS += frRate
        except:
            traceback.print_exc()

        if surfaces:
            [x.unlock() for x in surfaces]

        print 'VPlayer: Video thread has stopped'
Exemplo n.º 13
0
def encodeVideo(fName, fOutFile, codecName):
    # ------------------------------------
    import time, traceback
    import pymedia.muxer as muxer
    import pymedia.audio.acodec as acodec
    import pymedia.video.vcodec as vcodec

    format = fName.split('.')[-1].lower()
    dm = muxer.Demuxer(format)
    f = open(fName, 'rb')
    fw = open(fOutFile, 'wb')
    s = f.read(390000)
    r = dm.parse(s)

    print "Streams found:", dm.streams
    ss = filter(lambda x: x['type'] == muxer.CODEC_TYPE_AUDIO, dm.streams)
    if len(ss) == 0:
        raise 'No suitable audio streams in a file'
    a_id = ss[0]['index']
    ac = acodec.Decoder(ss[0])

    ss = filter(lambda x: x['type'] == muxer.CODEC_TYPE_VIDEO, dm.streams)
    if len(ss) == 0:
        raise 'No video streams in a file'
    v_id = ss[0]['index']
    vc = vcodec.Decoder(ss[0])

    #create muxer
    mux = muxer.Muxer('ts')
    header = a_id1 = v_id1 = None
    m_data = []
    while len(s):
        for d in r:
            if d[0] == v_id:
                vfr = vc.decode(d[1])
                if vfr:
                    if v_id1 == None:
                        params = vc.getParams()
                        params['id'] = vcodec.getCodecID(codecName)
                        if codecName == 'mpeg1video':
                            params['bitrate'] = 2700000
                        elif codecName == 'mpeg2video':
                            params['bitrate'] = 9800000

                        print 'Video: encoder params ', params
                        vc1 = vcodec.Encoder(params)
                        v_id1 = mux.addStream(muxer.CODEC_TYPE_VIDEO,
                                              vc1.getParams())
                        print "video stream id in muxer:", v_id1

                    s1 = vc1.encode(vfr)
                    m_data.append((v_id1, s1))
            elif d[0] == a_id:
                frame = ac.decode(d[1])
                if a_id1 == None:
                    params = {
                        'channels': frame.channels,
                        'sample_rate': frame.sample_rate,
                        'bitrate': frame.bitrate,
                        'id': acodec.getCodecID('mp3')
                    }
                    print 'Audio: encoder params ', params
                    ac1 = acodec.Encoder(
                        params)  #or any other supported audio encoder
                    a_id1 = mux.addStream(muxer.CODEC_TYPE_AUDIO,
                                          ac1.getParams())
                    print "audio stream id in muxer:", a_id1

                encFrames = ac1.encode(frame.data)
                for fr in encFrames:
                    m_data.append((a_id1, fr))

        # We know that video and audio exists already
        if a_id1 != None and v_id1 != None:
            # Start header when all streams are captured
            if header == None:
                header = mux.start()
                fw.write(header)

            for st_id, ss in m_data:
                s1 = mux.write(st_id, ss)
                if s1:
                    fw.write(s1)

            m_data = []

        s = f.read(100000)
        r = dm.parse(s)

    fw.close()
Exemplo n.º 14
0
  w= v[ 0 ][ 'width' ]
  h= v[ 0 ][ 'height']
  if video==1:
   print '\nAssume video stream at %d index: ' % v_id
  if audio==1:
   print '\nAssume audio stream at %d index: ' % v_id
#  print 'Press ENTER to continue...'
#  sys.stdin.read(1)
  stream=dm.streams[ v_id ]
  if video==1 and nodecode==0 :
   pygame.display.set_mode( (800,600), 0 )
   overlay= None
  try:
   if stream[ 'id' ]!= 2:
    stream[ 'id' ]= stream[ 'id' ]+ 1
   c= vcodec.Decoder(stream)
  except:
   print "decoder id [%d] failed",stream[ 'id' ]
  frcnt=0
  dlen=0
  
  while len( s )> 0:
    dlen=dlen+len(s)
    if nodecode==1:
      print "demuxed %d " % dlen 
    for fr in r:
      if fr[ 0 ]== v_id and nodecode==0:
        try:
         d= c.decode( fr[ 1 ] )  	
         if d and d.data:
          if video==1:
Exemplo n.º 15
0
  def vid( self, queue):
    tr =0
    frRate= 0
    vc= surfaces= None
    stsDelay= sndDelay= lastPts= pts= vPTS= vpPts= cPts = hurry= 0
    clock= 0
    delay= .0
    print 'VPlayer: Video thread has started'
    try:
      while 1:
        # Video stream processing
        d= queue.get()
        if type( d )== str:
          if d== 'SEEK':
            hurry = 1
            if vc: vc.reset()
            sndDelay= -.5
          if d== 'STOP':
            break
          if d== 'START':
            self.overlay= None
            params= queue.get()

            try:
              #print 'External codec params are:', params
              vc= pymedia.video.ext_codecs.Decoder( params ) 
              sndDelay= 0
            except:
              print 'Falling back to software decoder'
              try:
                vc= vcodec.Decoder( params )
                sndDelay= 0
              except:
                traceback.print_exc()
                self.err.append( sys.exc_info()[1] )
                continue  
            clock= time.time()
          continue
        
        if self.playingFile== None or len( self.err )> 0 or self.seek:
          continue
        
        if vc== None:
          continue
        
        try:
          vfr= vc.decode( d[ 1 ] , d[ 3 ])
        except:
          cls= sys.exc_info()[ 0 ].__name__
          if cls.find( 'NeedBuffer' )!= -1:
            params= vc.getParams()
            #print 'External codec need buffers', params
            size= params[ 'width' ], params[ 'height' ]
            surfaces= [ pygame.Surface( size, 0, YV12 ) for x in range( 4 ) ]
            st= [ x.lock() for x in surfaces ]
            offs= [ x.get_offset() for x in surfaces ]
            #print 'Surfaces allocated', offs
            vc.setBuffers( st[ 0 ][ 1 ], offs )
            vfr= vc.decode( d[ 1 ], d[ 3 ]  )
          else:
            raise      
        if vfr and vfr.data:
          if self.skipSound:
            self.frameNum= 0
            self.skipSound= 0
            vPTS= 0
#24000/1001 (23.976) - 3754
# 24 3750
# 25 3600
# 30000/1001 (29.97)   3003");
# 30
# 50
# 60000/1001 (59.94)
# 60
# 0.1 - Í
          cPts = 1    
          if vfr.rate_base == 1001:
              if vfr.rate == 30000:
                  cPts = 0.03336666666666
              if vfr.rate == 24000:
                  cPts = 0.04170833333333
              if vfr.rate == 60000:    
                  cPts = 0.01668333333333
          if vfr.rate_base == 1:       
              if vfr.rate == 24:
                  cPts = 0.04166666666666
              if vfr.rate == 25:
                  cPts = 0.04
              if vfr.rate == 30:
                  cPts = 0.03333333333333
              if vfr.rate == 50:
                  cPts = 0.02
              if vfr.rate == 60:
                  cPts = 0.01666666666666                        
          if self.vBitRate == 0: self.vBitRate= vfr.bitrate
          if hurry > 0: 
              if hurry == 1:
                stsDelay = time.time()
                hurry == 2
              if vfr.pict_type == 1:
                hurry = 0  
          if self.overlayLoc and self.overlay== None:
            self.overlay= pygame.Overlay( YV12, vfr.size )
            if vfr.aspect_ratio > .0:
              self.pictureSize= ( vfr.size[ 1 ]* vfr.aspect_ratio, vfr.size[ 1 ] )
            else:
              self.pictureSize= vfr.size
            self.setOverlay( self.overlayLoc )   
          stsDelay = cPts - stsDelay + time.time()   
          if self.frameNum> -1:           	    
              if  (stsDelay )> 0:
                 time.sleep( stsDelay  )
          stsDelay = time.time()  
          if self.overlay and vfr.data :
               self.overlay.set_data( vfr.data )
          if(vfr.frame_pts > 0): print 'VPTS', float(vfr.frame_pts) / 90000   
          if (hurry == 0):
               self.overlay.display()
          self.frameNum+= 1
    except:
      traceback.print_exc()
    
    if surfaces:
      [ x.unlock() for x in surfaces ]
    
    print 'VPlayer: Video thread has stopped'