2012-11-22 14 views
1
내가는 FFmpeg 다시 문제가 오전

, 내가는 FFmpeg와 안돼서, 나는이를 연주 할 때 나는비디오 너무 빨리는 FFmpeg

...이 시간을 날짜까지 좋은 자습서를 찾을 수 없습니다 비디오가 ffmpeg로 재생되면 너무 빠르게 재생됩니다. ffmpeg는 FPS를 무시하고 있으며, 스레드 수면을 처리하고 싶지 않습니다. 비디오에는 FPS가 다릅니다.

AVPacket framepacket; 

while(av_read_frame(formatContext,&framepacket)>= 0){ 
    pausecontrol.lock(); 

    // Is it a video or audio frame¿? 
    if(framepacket.stream_index==gotVideoCodec){ 
     int framereaded; 
     // Video? Ok 
     avcodec_decode_video2(videoCodecContext,videoFrame,&framereaded,&framepacket); 
     // Yeah, did we get it? 
     if(framereaded && doit){ 
      AVRational millisecondbase = {1,1000}; 
      int f_number = framepacket.dts; 
      int f_time = av_rescale_q(framepacket.dts,formatContext->streams[gotVideoCodec]->time_base,millisecondbase); 
      currentTime=f_time; 
      currentFrameNumber=f_number; 

      int stWidth = videoCodecContext->width; 
      int stHeight = videoCodecContext->height; 
      SwsContext *ctx = sws_getContext(stWidth, stHeight, videoCodecContext->pix_fmt, stWidth, 
      stHeight, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); 
      if(ctx!=0){ 
      sws_scale(ctx,videoFrame->data,videoFrame->linesize,0,videoCodecContext->height,videoFrameRGB->data,videoFrameRGB->linesize); 
      QImage framecapsule=QImage(stWidth,stHeight,QImage::Format_RGB888); 

      for(int y=0;y<stHeight;y++){ 
       memcpy(framecapsule.scanLine(y),videoFrameRGB->data[0]+y*videoFrameRGB->linesize[0],stWidth*3); 
      } 
      emit newFrameReady(framecapsule); 
      sws_freeContext(ctx); 
      } 

     } 
    } 
    if(framepacket.stream_index==gotAudioCodec){ 
     // Audio? Ok 
    } 
    pausecontrol.unlock(); 
    av_free_packet(&framepacket); 
} 

모든 아이디어 :

난 당신이 루프를 찾을 수 있습니다,이 스레드를 생성?

+0

비디오에서 FPS 정보를 얻을 수 있습니까? – nhahtdh

+0

예. 저 할 수 있어요. 글쎄, 나는 인터넷에서 찾고있다. 스레드 수면은 어떤 생각 일 수 있는가? 스레드를 잠 그어도 오디오를 재생해야합니다. 오디오 디코딩도 또한 악화 될 것입니다 ... – Spamdark

+0

이 작업을 수행하는 데 경험이 없지만 렌더링 및 디코딩 단계를 분리 할 수 ​​있습니까? (렌더링은 1/FPS 초마다 잠자기 후 디코딩 된 버퍼에서 소비되며 버퍼가 채워질 때까지 디코딩 단계가 계속됩니다 - 소비자 - 생산자 종류의 구조). – nhahtdh

답변

2

간단한 용액

firstFrame = true; 
for(;;) 
{ 
    // decoding, color conversion, etc. 

    if (!firstFrame) 
    { 
    const double frameDuration = 1000.0/frameRate; 
    duration_t actualDelay = get_local_time() - lastTime; 
    if (frameDuration > actualDelay) 
     sleep(frameDuration - actualDelay); 
    } 
    else 
    firstFrame = false; 

    emit newFrameReady(framecapsule); 

    lastTime = get_local_time(); 
} 

get_local_time()duration_t 및 추상적 인 FPS 값에 기초하여 지연 시간을 사용하는 것이다.

보다 정확한 방법은 각 프레임에 타임 스탬프를 사용하는 것이지만 아이디어는 동일합니다.

+0

대단히 감사합니다! – Spamdark

+0

안녕하세요. 프레임이 너무 빠르거나 너무 느릴 때 비디오가 네트워크를 통해 스트리밍 될 때 수행 할 작업에 대한 제안? – Nav

+0

@Nav 수신 된 패킷을 (개별 스레드에서) 큐에 넣고 필요에 따라 추출합니다. – pogorskiy

관련 문제