Read and Write Tags of Music Files with FFmpeg

本贴最后更新于 1749 天前,其中的信息可能已经渤澥桑田

In both my previous and recent projects, I have been working with tags (metadata) of music files. One of the reason being I am rather particular about having a nicely organised library with all tag data aligned to the same format. Until recently while I was seeking for a solution to read and write tags of (potentially) all music formatsI only have MP3, FLAC, AIFF and M4A in my library, so that’s kinda all for me., and I encountered FFmpeg, the Swiss Army Knife of media processing.

FFmpeg has always been my go-to solution for processing media programmatically or in batch, and I have recently found the way to write into the tags of music files using it. The way of doing so might be a little verbose as everything have to fit into the command line interface with other components.

Read tags

ffprobe -show_format -print_format json aiffout.aiff
ffprobe version 4.2.1 Copyright (c) 2007-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.8)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1_2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include/darwin -fno-stack-check' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
{
Input #0, aiff, from 'aiffout.aiff':
  Metadata:
    title           : shake it!
    artist          : emon feat. 初音ミク.鏡音リン.鏡音レン
    album           : 「マジカルミライ 2014」OFFICIAL ALBUM
    compilation     : 1
    encoded_by      : Max 0.9.1
    title-sort      : shake it!
    album-sort      : 「まじかるみらい 2014」OFFICIAL ALBUM
    TDTG            : 2014-11-03T15:38:58
    artist-sort     : emon feat sort
    encoder         : Lavf58.29.100
  Duration: 00:03:47.03, start: 0.000000, bitrate: 2822 kb/s
    Stream #0:0: Audio: pcm_s32be, 44100 Hz, 2 channels, s32, 2822 kb/s
    "format": {
        "filename": "aiffout.aiff",
        "nb_streams": 1,
        "nb_programs": 0,
        "format_name": "aiff",
        "format_long_name": "Audio IFF",
        "start_time": "0.000000",
        "duration": "227.027528",
        "size": "80095752",
        "bit_rate": "2822415",
        "probe_score": 100,
        "tags": {
            "title": "shake it!",
            "artist": "emon feat. 初音ミク.鏡音リン.鏡音レン",
            "album": "「マジカルミライ 2014」OFFICIAL ALBUM",
            "compilation": "1",
            "encoded_by": "Max 0.9.1",
            "title-sort": "shake it!",
            "album-sort": "「まじかるみらい 2014」OFFICIAL ALBUM",
            "TDTG": "2014-11-03T15:38:58",
            "artist-sort": "emon feat sort",
            "encoder": "Lavf58.29.100"
        }
    }
}

Write tags

ffmpeg -i aiff.aiff -map 0 -y -codec copy -write_id3v2 1 -metadata "artist-sort=emon feat sort" aiffout.aiff
ffmpeg version 4.2.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.8)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.2.1_2 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home/include/darwin -fno-stack-check' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-libsoxr --enable-videotoolbox --disable-libjack --disable-indev=jack
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, aiff, from 'aiff.aiff':
  Metadata:
    title           : shake it!
    artist          : emon feat. 初音ミク.鏡音リン.鏡音レン
    album           : 「マジカルミライ 2014」OFFICIAL ALBUM
    compilation     : 1
    encoded_by      : Max 0.9.1
    title-sort      : shake it!
    creation_time   : 2014-11-03T15:38:58
    TDTG            : 2014-11-03T15:38:58
    album-sort      : 「まじかるみらい 2014」OFFICIAL ALBUM
    artist-sort     : emon feat. はつねみく.かがみねりん.かがみねれん
  Duration: 00:03:47.03, start: 0.000000, bitrate: 2822 kb/s
    Stream #0:0: Audio: pcm_s32be, 44100 Hz, stereo, s32, 2822 kb/s
Output #0, aiff, to 'aiffout.aiff':
  Metadata:
    title           : shake it!
    artist          : emon feat. 初音ミク.鏡音リン.鏡音レン
    album           : 「マジカルミライ 2014」OFFICIAL ALBUM
    compilation     : 1
    encoded_by      : Max 0.9.1
    title-sort      : shake it!
    album-sort      : 「まじかるみらい 2014」OFFICIAL ALBUM
    TDTG            : 2014-11-03T15:38:58
    artist-sort     : emon feat sort
    encoder         : Lavf58.29.100
    Stream #0:0: Audio: pcm_s32be (NONE / 0x454E4F4E), 44100 Hz, stereo, s32, 2822 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
size=   78219kB time=00:03:47.02 bitrate=2822.5kbits/s speed=2.08e+03x
video:0kB audio:78218kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000549%
 

Read cover art

Write cover art

  • FFmpeg

    FFmpeg 是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。

    23 引用 • 32 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...