h264 裸流打包成mp4 注意事項

來源:互聯網
上載者:User

需求: Android 端把網路攝影機的一段現正播放的視頻流,截取儲存成mp4(按錄影按鈕時開始錄影)。

實現: ffmpeg + x264 + sdl;

h264 裸流 打包成MP4,在網上也有一大堆文章,ffmpeg 也有一個muxing 的 example,大致流程都是一樣的,參考ffmpeg的muxing.c 就可以寫一個。我這裡把我在這個過程中遇到的問題寫出來,這些問題困擾了我很久才解決,誰叫我是視頻方面的小白呢。

這三個問題其實很簡單,但如果對這方面不瞭解的話,耗掉的時間還是很多的。

 

1,dts,pts:

     在write_frame()之前,每一個avpacket 資料都要設定 dts,pts,因為我的視訊沒有B幀,所以dts = pts 便可;pts 一開始我也不知道要怎麼設, 參考muxing.c,設定成寫入的幀數量便可(結合問題3);

2,sps,pps;

     我接受到的裸流裡面 sps  +pps + i幀 是放在一個NALU 裡面的,在儲存成mp4時這個sps 非常重要,我一開始沒有設定,打包後的Mp4 普通的播放器就不能識別;

     sps 在建立 編碼器時候傳遞給編碼器;     sps 是 00 00 00 01 後面的,不包含這個00 00 00 01 這個碼;pps 我還沒用到。   [plain]  view plain  copy   case AVMEDIA_TYPE_VIDEO:       c->codec_id = codec_id;       LOGE("add_stream AVMEDIA_TYPE_VIDEO c->time_base.num = %d",ost->st->codec->time_base.num);       c->bit_rate = 400000;       /* Resolution must be a multiple of two. */       c->width    = 352;       c->height   = 288;       /* timebase: This is the fundamental unit of time (in seconds) in terms        * of which frame timestamps are represented. For fixed-fps content,        * timebase should be 1/framerate and timestamp increments should be        * identical to 1. */       ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE};//STREAM_FRAME_RATE };       c->time_base       =ost->st->time_base;          c->gop_size      = 12; /* emit one intra frame every twelve frames at most */       c->pix_fmt       = STREAM_PIX_FMT;       if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {           /* just for testing, we also add B frames */           c->max_b_frames = 2;       }       if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {           /* Needed to avoid using macroblocks in which some coeffs overflow.            * This does not happen with normal video, it just happens here as            * the motion of the chroma plane does not match the luma plane. */           c->mb_decision = 2;       }      c->extradata = spsinfo;      c->extradata_size = 10;   break;  


3,fps:

    在儲存成mp4後,調試過程中各種fps都出來了,最後仔細看muxing.c ,發現其實要設定正確很簡單:在pts 按1遞增後,用一個很簡單的函數就解決了這個問題。

   c->time_base : 輸入的幀率fps = 25 :{1,25},ost->st->time_base : 輸出的幀率; 我這裡設定和輸入的一樣就好了。

  [plain]  view plain  copy    packet->pts = (ptsInc++);;// * (90000/STREAM_FRAME_RATE);     packet->dts = packet->pts;   // packet->duration = 8/1000000;    // packet->pos = -1;//ptsInc;           av_packet_rescale_ts(packet, c->time_base, ost->st->time_base);  



http://blog.csdn.net/xqt8888/article/details/43152529


muxing.c http://ffmpeg.org/doxygen/trunk/doc_2examples_2muxing_8c-example.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.