Before writing HLS video merge into one MP4 video: M3u8-ts files into MP4 files HLS Open lesson (5)HLS Integrated into a whole video to solve the problem of DTS discontinuous, some people are sure to say that multiple MP4 videos into one MP4 video and HLS integrated into one MP4 video is the same? It’s actually different. Here are a few reasons:

  • HLS is essentially a video, so there is no problem that multiple videos are connected to PTS and DTS. PTS and DTS in multiple TS files have helped to map out the internal relationship
  • Even though there may be PTS and DTS problems with internal TS files, this is usually a problem caused by slice exceptions, not a problem that HLS composited MP4 has to solve
  • Several new MP4 files are combined into one MP4 file, and the PTS and DTS at the junction of the files require special care.


Making multiple MP4 videos into one MP4 video is now widely used in short video applications: the main applications are as follows.

  • In the process of video recording, users can choose to segment recording, which will produce multiple MP4 video files, and the generated video needs to generate an MP4 file
  • The AUDIO and video editing SDK allows you to import multiple videos and process each video separately to produce a single video


  • The entire operation process is just decapsulation and reencapsulation, there should be no decoding and coding, because the decoding and coding of mobile terminal is very time-consuming, and the user experience can not be overcome.
  • The merged video should be in the same encapsulation format and resolution, otherwise the merged video will appear abnormal. Fortunately, the video imported by the mobile terminal is shot on the mobile phone, so the above two conditions should be able to meet.

How do I resolve the PTS and DTS issues of the merge process

Remember PTS and DTS at the end of the file

When executing av_read_frame, if the end of the file is read, EOF appears:

AVPacket packet; int result = av_read_frame(input_context_, &packet); if (result < 0) { av_packet_unref(&packet); If (result == AVERROR_EOF) {current_video_index_++; if (result == AVERROR_EOF) {current_video_index_++; If (current_video_index_ >= input_size_) {current_video_index_ >= input_size_; } int ret; do { ret = OpenInputVideo(current_video_index_); if (ret ! = 0) { current_video_index_++; } } while (ret ! = 0); continue; } continue; }Copy the code

When the end of the file is read, the next file is read.

The next file interconnects with the PTS and DTS of the previous file

Some key data saved globally:

    int64_t last_video_pts_;
    int64_t last_video_dts_;
    int64_t last_audio_pts_;
    int64_t last_audio_dts_;

    int64_t current_video_pts_;
    int64_t current_video_dts_;
    int64_t current_audio_pts_;
    int64_t current_audio_dts_;
Copy the code

The PTS and DTS information of the last video is saved, and the PTS and DTS information of the current video is recorded. In this way, the PTS and DTS information is constantly checked when the video is decapsulated and encapsulated, so as to ensure that the PTS and DTS information is properly arranged.

Note the rotation Angle of the original video

Check the rotation Angle information of each video. We need to read this information at the beginning of the video and set it into the output video metadata, because the rotation Angle information also needs to be consistent, otherwise there will be problems in the synthesized video.

        if (input_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
            AVDictionaryEntry *rotateEntry = av_dict_get(input_stream->metadata, "rotate", nullptr, 0);
            if (rotateEntry != nullptr) {
                int rotate = atoi(rotateEntry->value);
                if (rotate != 0) {
                    char rotate_str[1024];
                    sprintf(rotate_str, "%d", rotate);
                    av_dict_set(&output_stream->metadata, "rotate", rotate_str, 0);
                }
            }
        }
Copy the code

The rotation Angle of the original video is recorded and written into the metadata of the generated file, so that the rotation Angle of the generated file is the same as that of the original video.

The project is open source

Open source project reference: github.com/JeffMony/Je…

In the open source project, a test case is provided: there are 4 videos under the files folder, in which inputt1.mp4, inputT2.mp4 and input3.mp4 are merged into output.mp4 video, please refer to below

Input1.mp4 Duration: 00:00:10.86, start: 0.000000, bitrate: 11161kb /s Input2.mp4 Duration: 00:00:11.22, start: Mp4 Duration: 00:00:11.46, start: 0.000000, bitrate: 1079KB /s Input3.mp4 Duration: 00:00:11.46, start: 0.000000, bitrate: 1137kb /s output.mp4 Duration: 00:00:33.63, start: 0.000000, bitrate: 10513kb /sCopy the code

Can see the output. Mp4 is indeed input1. Mp4 / input2. Mp4 / input3. Mp4 video three merger.