[FFmpeg-devel] [PATCH] libavformat/mp3enc: Notify to add "-write_xing false" in CBR mode

sharpbai at gmail.com sharpbai at gmail.com
Sun Apr 23 07:11:51 EEST 2017

From: sharpbai <sharpbai at gmail.com>

Encoding a CBR mp3 file without "-write_xing false" may result in
mp3 file duration incorect on ios safari browser and webview.
I try to fix it but it can’t be done as mp3 muxer don’t know
it is a CBR file until encode process finished. And it’s hard
to remove the first frame at that time. Only the mp3 encoder
(such as libmp3lame) know it is CBR or not before encoding start.

Bug example:

ffmpeg -i a.mp3 -c:a mp3 -ab 32k -ar 44100 -ac 1 b.mp3

The duration of the generated file b.mp3 is wrong on ios safari
browser from ios7 to ios10.

Working example:
ffmpeg -i a.mp3 -c:a mp3 -ab 32k -ar 44100 -ac 1 \
-write_xing false b.mp3
 libavformat/mp3enc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index 3cbf7bd..a55dbf4 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -395,8 +395,13 @@ static void mp3_update_xing(AVFormatContext *s)
     int i, rg_size;
     /* replace "Xing" identification string with "Info" for CBR files. */
-    if (!mp3->has_variable_bitrate)
+    if (!mp3->has_variable_bitrate) {
         AV_WL32(mp3->xing_frame + mp3->xing_offset, MKTAG('I', 'n', 'f', 'o'));
+        av_log(s, AV_LOG_WARNING,
+            "This is a CBR mp3 file. But its first frame header might be wrong, "
+            "which result in file duration incorrect on ios browser. "
+            "Please add \"-write_xing false\" to avoid this problem.\n");
+    }
     AV_WB32(mp3->xing_frame + mp3->xing_offset + 8,  mp3->frames);
     AV_WB32(mp3->xing_frame + mp3->xing_offset + 12, mp3->size);

More information about the ffmpeg-devel mailing list