[問題] ffmpeg跳格或倒撥

看板C_and_CPP作者 (newJoey)時間9年前 (2014/07/22 14:40), 編輯推噓2(202)
留言4則, 1人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) FFMpeg 問題(Question): 想把影片倒撥與順撥 但每次進入10毫秒的迴圈讀取(av_read_fream讀取) 是用av_seek_frame搜影片一倍速時 發現明顯播放速度不對(太慢) 餵入的資料(Input): 以下是抓取fps、duration的方法。 AVFormatContext *pFormatCtx; AVCodecContext *pCodecCtx; int videoStream; int fps; int64_t duration; if( avformat_open_input(&pFormatCtx, fileName.c_str(), NULL, NULL) != 0) return AVAFRetValue::IO_ERR; if(avformat_find_stream_info(pFormatCtx, NULL) < 0) return AVAFRetValue::IO_ERR; av_dump_format(pFormatCtx, 0, "avi", 0); videoStream=-1; for(int i = 0; i < pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if(videoStream == -1) return AVAFRetValue::IO_ERR; // Didn pCodecCtx=pFormatCtx->streams[videoStream]->codec; pCodec=avcodec_find_decoder(pCodecCtx->codec_id); if(pCodec==NULL) { return AVAFRetValue::IO_ERR; } if(avcodec_open2(pCodecCtx, pCodec, NULL)<0) return AVAFRetValue::IO_ERR; int64_t duration = pFormatCtx->duration/1000000; fps = pCodecCtx->pkt_timebase.den; 預期的正確結果(Expected Output): 期望能在撥放時能有正確的撥放速度 錯誤結果(Wrong Output): 每次用都下av_seek_frame會使得畫面變慢 是否是milliseconds設法錯誤? 程式碼(Code):(請善用置底文網頁, 記得排版) if(direction==PlayDirection::FORWARD) { if( speed == PlaySpeed::X1) milliseconds += (1.0/fps*1000); if( speed == PlaySpeed::X2) milliseconds += (2.0/(fps*1000)); if( speed == PlaySpeed::X4) milliseconds += (4.0/(fps*1000)); if( speed == PlaySpeed::X8) milliseconds += (8.0/fps*1000); if( speed == PlaySpeed::X16) milliseconds += (16.0/fps*1000); if ( milliseconds >= duration*1000 ) { milliseconds = duration*1000; } } else { if( speed == PlaySpeed::X1) milliseconds -= (1.0/fps); if( speed == PlaySpeed::X2) milliseconds -= (2.0/fps); if( speed == PlaySpeed::X4) milliseconds -= (4.0/fps); if( speed == PlaySpeed::X8) milliseconds -= (8.0/fps); if( speed == PlaySpeed::X16) milliseconds -= (16.0/fps); if( milliseconds <= -duration*1000 ) { milliseconds = -duration*1000 ; } } av_init_packet(&packet); packet.data = NULL; packet.size = 0; ////////////////搜尋影格////////////////// if(av_seek_frame(pFormatCtx,-1,milliseconds*1000,AVSEEK_FLAG_ANY)<0) return &packet; while(av_read_frame(pFormatCtx, &packet)>=0) { if(packet.stream_index==videoStream) { return &packet; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.32.153 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1406011236.A.A87.html

07/23 00:28, , 1F
猜測是seek本身就很慢 試看看seek後解一個packet要多久
07/23 00:28, 1F


07/23 00:42, , 3F
你timestamp填的單位應該不對
07/23 00:42, 3F

07/23 00:44, , 4F
上面網頁 查av_seek_frame不是以秒為單位
07/23 00:44, 4F
文章代碼(AID): #1JpWTag7 (C_and_CPP)