H264 Bare stream packaging into MP4 considerations

Source: Internet
Author: User
Requirements: The Android end captures and saves a video stream of the webcam as an mp4 (recording starts when the record button is pressed).

Implementation: ffmpeg + x264 + sdl;

h264 naked stream is packaged into MP4, there are a lot of articles on the Internet, ffmpeg also has a muxing example, the general process is the same, refer to muxing.c of ffmpeg to write one. Here I write out the problems I encountered in the process. These problems have troubled me for a long time to solve. Who calls me a video white?

These three questions are actually very simple, but if you don't understand this, you will still spend a lot of time.

Zh

1, dts, pts:

Before write_frame (), every avpacket data must be set with dts, pts, because my video does not have B frames, so dts = pts. I do n’t know how to set pts at the beginning. The number of frames written is sufficient (combined with question 3);

2, sps, pps;

The naked stream I received is sps + pps + i frame is placed in a NALU, this sps is very important when saving to mp4, I did not set it at the beginning, the packaged Mp4 ordinary player could not recognize it;

The SPS is passed to the encoder when creating the encoder; the SPS is after 00 00 00 01 and does not include the code 00 00 00 01; I have not used pps. Zh

    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:

After saving to mp4, all kinds of fps came out during the debugging process. Finally, looking at muxing.c carefully, I found that it is actually very simple to set it right: after pts is incremented by 1, a very simple function solves this problem.

C-> time_base: input frame rate fps = 25: {1,25}, ost-> st-> time_base: output frame rate; I set it here as well as the input.

Zh

       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);
————————————————
Copyright statement: This article is the original article of the CSDN blogger "xqt8888", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: https://blog.csdn.net/xqt8888/article/details/43152529

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.