[FFmpeg-devel] [PATCH] mp3enc: avoid truncating id3v1 tags by one byte

Tobias Rapp t.rapp at noa-audio.com
Mon Oct 31 15:11:37 CET 2011


Avoid writing the trailing null-byte for id3v1 tags if length reaches max length.

To reproduce the issue one can use a command like:

$ ffmpeg -i /tmp/test01.wav \
-metadata title=MyTitle89012345678901234567890 \
-metadata artist=MyArtist9012345678901234567890 \
-metadata album=MyAlbum89012345678901234567890 \
-metadata comment=MyComment012345678901234567890 \
-metadata track=01/10 -metadata date=2001-01-01 \
-write_id3v1 1 /tmp/test01.mp3

the desired output is (see http://id3.org/ID3v1):

$ tail --bytes=128 /tmp/test01.mp3 | xxd
0000000: 5441 474d 7954 6974 6c65 3839 3031 3233  TAGMyTitle890123
0000010: 3435 3637 3839 3031 3233 3435 3637 3839  4567890123456789
0000020: 304d 7941 7274 6973 7439 3031 3233 3435  0MyArtist9012345
0000030: 3637 3839 3031 3233 3435 3637 3839 304d  678901234567890M
0000040: 7941 6c62 756d 3839 3031 3233 3435 3637  yAlbum8901234567
0000050: 3839 3031 3233 3435 3637 3839 3032 3030  8901234567890200
0000060: 314d 7943 6f6d 6d65 6e74 3031 3233 3435  1MyComment012345
0000070: 3637 3839 3031 3233 3435 3637 3800 01ff  6789012345678...


---
 libavformat/mp3enc.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c
index 793c9c6..f02872d 100644
--- a/libavformat/mp3enc.c
+++ b/libavformat/mp3enc.c
@@ -55,11 +55,12 @@ static int id3v1_create_tag(AVFormatContext *s, uint8_t *buf)
     buf[0] = 'T';
     buf[1] = 'A';
     buf[2] = 'G';
-    count += id3v1_set_string(s, "TIT2",    buf +  3, 30);       //title
-    count += id3v1_set_string(s, "TPE1",    buf + 33, 30);       //author|artist
-    count += id3v1_set_string(s, "TALB",    buf + 63, 30);       //album
-    count += id3v1_set_string(s, "TDRL",    buf + 93,  4);       //date
-    count += id3v1_set_string(s, "comment", buf + 97, 30);
+    /* we knowingly overspecify each tag length by one byte to compensate for the mandatory null byte added by av_strlcpy */
+    count += id3v1_set_string(s, "TIT2",    buf +  3, 30 + 1);       //title
+    count += id3v1_set_string(s, "TPE1",    buf + 33, 30 + 1);       //author|artist
+    count += id3v1_set_string(s, "TALB",    buf + 63, 30 + 1);       //album
+    count += id3v1_set_string(s, "TDRL",    buf + 93,  4 + 1);       //date
+    count += id3v1_set_string(s, "comment", buf + 97, 30 + 1);
     if ((tag = av_dict_get(s->metadata, "TRCK", NULL, 0))) { //track
         buf[125] = 0;
         buf[126] = atoi(tag->value);
-- 
1.7.0.4



More information about the ffmpeg-devel mailing list