Discussion:
[Libav-user] Issues when encoding and muxing Mpeg4 into Mp4 container
James
2018-11-23 19:19:32 UTC
Permalink
Hi everyone,

I am using the FFmpeg project's C libraries to decode, modify and then
re-encode the frames with new content.
I need to encode using a codec included in the LGPL version of FFmpeg, as
such, I picked "mpeg4". I also need to encode using the MP4 container.
The parts that do the decoding and frame conversions are working as
intended, however, I am having an issue with the encoding.

The issue I am having is that using this combination of video codec and
container, there are some issues with the output:
1- The MP4 file is unreadable from Adobe Premiere, Windows Media Player,
Films and TV on Windows 10, and Gnome Totem player to give a few. Only VLC
media player is able to play it. There are complaints from ffprobe about
damaged MP4 headers.
2- ffmpeg and ffprobe CLIs themselves will not decode the file's frames,
only headers. See attached gist for complete output of ffprobe, but
basically the biggest issue is that width/coded_width=0,
heigh/coded_height=0 and as such decoding fails. Also, a cryptic warning
about "time_increment_bits", and various warnings from the mpeg4 decoder.

Interestingly, issue 2 only happens when I am using mpeg4 codec, with MP4
container. It does not happen if I use libx264 in a GPL build like Zeranoe
build, or use MKV container. Coded_width and such are set in the headers in
that case.
Additionally, when using libx264, I can fix issue 1 by remuxing the .mp4
file with the ffmpeg CLI (ffmpeg -i in.mp4 -c copy out.mp4) and the file
can then be read by Premiere, Films and TV etc.. But not when using mpeg4
codec.

When developing the application, I tried following the examples, seeing how
existing projects are using the libraries, and also following the ffmpeg
CLI application in the debugger to figure out if it was setting some flag I
didn't know I needed to set. I couldn't find the culprit.
My project is in C++ and as such I made wrapper classes which use the C
API, but I made a cut down example in C which shows the issue (attached
below).

I have tested the example has the same issues in the following combinations
of platforms/versions:
- WIndows 10 (MinGW GCC build, shared libs) / Ubuntu 18.04 (GCC build,
static libs)
- FFmpeg versions: Git head, 4.1.0, 4.0.1, 3.4.2
- Prebuilt Zeranoe build (https://ffmpeg.zeranoe.com/builds/) in case my
build was the culprit.

There is obviously something I am doing wrong when using the API. Can
someone point me in the right direction to fix my issues?
Here are all the details I can think of that might be useful:

The source code for my cut down application (encodes 10 seconds of a green
AVFrame):
https://gist.github.com/jamesc2048/7503ec5dc3b46f3e687894b55192b6ff

Outputs from ffprobe on MP4 file:
https://gist.github.com/jamesc2048/bf213c773d5dbcb4f19f22d1cea78ec0

Output of the application (the video file itself):
https://drive.google.com/file/d/1hzui9D8ZX1XxqTgP_vGaGjPWBWXefhBr/view?usp=sharing

FFmpeg compilation flags and details:
https://gist.github.com/jamesc2048/4463b498277c575198c982c6fd758616

Thanks in advance for the help,

- James

Loading...