[FFmpeg-user] Duplicate frames when extracting PNGs from fragmented mp4

Valentin Schweitzer vs at nativewaves.com
Wed Nov 13 21:02:44 EET 2019


Hi,

I am trying to extract each frame of a fragmented mp4 sequence as PNGs.
The issue is, I am getting more frames than expected, due to some
frames being duplicated. For example, from a sequence of mp4 fragments
containing 800 frames, 803 might be generated. This issue only appears
when an audio track is present, adding -an fixes it. The example below
uses a fragmented mp4 sequence containing 1 frame to reproduce the issue.

To reproduce:

For better readability, all ffmpeg outputs are attached.
Start off with the file included in [1] and convert it to fragmented
mp4, using only the first frame:
ffmpeg.exe -y -i .\trailer_iphone.m4v -frames:v 1 -acodec aac -vcodec h264 -g 10 -r 25 -rc cbr_ld_hq -hls_segment_type fmp4 -hls_time 0 -hls_init_time 0.04 -hls_flags split_by_time+second_level_segment_index -strftime 1 -hls_segment_filename stream-%%18d.m4s -hls_fmp4_init_filename init.mp4 -hls_playlist_type event index.m3u8
# Output of this command is in "generate_fragmented_mp4.txt"

Use ffprobe to count the frames in the resulting video:
ffprobe.exe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 index.m3u8
# Output of this command is in "counting_frames.txt".
# The output is "1\n1", meaning the stream contains 1 frame.

Extract the frames as PNGs:
ffmpeg.exe -y -i index.m3u8 test%18d.png
# Output of this command is in "extracting_pngs.txt"

This creates two identical files, named "test000000000000000001.png"
and "test000000000000000002.png".

If anyone knows why these duplicates appear or how to avoid
them, any advice would be much appreciated.

Additional info:

Changing the number of frames to include in the fragmented mp4
sequence changes the behavior. Using for example 100 frames
(-frames:v 100), only 98 frames (stream-000000000000000000.m4s to
stream-000000000000000097.m4s) are written, which is confirmed by
the ffprobe command listed above. When then extracted to PNGs,
ffmpeg writes 100 files, listing 3 dups and 1 drop. Logs of
generating a 100 frame long fmp4 file and extracting PNGs
are included as "generate_fmp4_100_frames.txt" and
"extract_100_pngs.txt" respectively.

With a test video produced by muxing the first frame from a PNG
and the audio track from the original trailer (see [1]) with the
-shortest option the issue does not occur.

Extracting the audio from the trailer:
ffmpeg .\trailer_iphone.m4v audio.mp3

Muxing a single frame and the audio:
ffmpeg.exe -y -i .\test000000000000000001.png -i .\audio.mp3 -shortest -frames:v 1 -acodec aac -vcodec h264 -g 10 -r 25 -rc cbr_ld_hq -hls_segment_type fmp4 -hls_time 0 -hls_init_time 0.04 -hls_flags split_by_time+second_level_segment_index -strftime 1 -hls_segment_filename stream-%%18d.m4s -hls_fmp4_init_filename init.mp4 -hls_playlist_type event index.m3u8

 From here on, continue with the commands in "To reproduce", starting
with the ffprobe command. This should only produce one PNG, when
extracted.

Regards,
Valentin

[1]:
The original trailer
https://nativewaves-my.sharepoint.com/:v:/p/vs/Eatq4tSgULdEg2u_3s-ctDcBV4jke0SHpzZHmSTWyXssIQ?e=y6LpdH
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: generate_fragmented_mp4.txt
URL: <https://ffmpeg.org/pipermail/ffmpeg-user/attachments/20191113/552529bd/attachment.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: counting_frames.txt
URL: <https://ffmpeg.org/pipermail/ffmpeg-user/attachments/20191113/552529bd/attachment-0001.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: extracting_pngs.txt
URL: <https://ffmpeg.org/pipermail/ffmpeg-user/attachments/20191113/552529bd/attachment-0002.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: generate_fmp4_100_frames.txt
URL: <https://ffmpeg.org/pipermail/ffmpeg-user/attachments/20191113/552529bd/attachment-0003.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: extract_100_pngs.txt
URL: <https://ffmpeg.org/pipermail/ffmpeg-user/attachments/20191113/552529bd/attachment-0004.txt>


More information about the ffmpeg-user mailing list