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

Submitted by sharpbai@gmail.com on April 23, 2017, 4:11 a.m.

Details

Message ID 1492920711-7569-1-git-send-email-sharpbai@gmail.com
State New
Headers show

Commit Message

sharpbai@gmail.com April 23, 2017, 4:11 a.m.
From: sharpbai <sharpbai@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(-)

Comments

Hendrik Leppkes April 23, 2017, 7:41 a.m.
On Sun, Apr 23, 2017 at 6:11 AM,  <sharpbai@gmail.com> wrote:
> From: sharpbai <sharpbai@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.
>

I don't think we should add a warning about every broken player out there.

- Hendrik

Patch hide | download patch | download mbox

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);