ffmpeg 给音频添加封面,ffmpeg对音视频metadata 相关操作

ffmpeg 给音频添加封面,ffmpeg对音视频metadata 相关操作

ffmpeg给音频添加封面的功能找了很久。在官网的文档中找到了方法。

但是因为各种原因,失败了很多次。

这次终于完全理解了这个命令的使用。

首先我们看到这个选项**-disposition[:stream_specifier]** value **(***output,per-stream***)**

这个disposition选项是用来调整文件布局用的,有以下这些选项:

default
dub
original
comment
lyrics
karaoke
forced
hearing_impaired
visual_impaired
clean_effects
attached_pic
captions
descriptions
dependent
metadata

我们可以看到,其中有一个attached_pic选项,这个就是我们加封面的时候要用的。

先看看官网给的其他例子

For example, to make the second audio stream the default stream:

ffmpeg -i in.mkv -c copy -disposition:a:1 default out.mkv

To make the second subtitle stream the default stream and remove the default disposition from the first subtitle stream:

ffmpeg -i in.mkv -c copy -disposition:s:0 0 -disposition:s:1 default out.mkv

然后就是添加封面的命令了

To add an embedded cover/thumbnail:

ffmpeg -i in.mp4 -i IMAGE -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic out.mp4

Not all muxers support embedded thumbnails, and those who do, only support a few formats, like JPEG or PNG.

可以看到,只支持很少的图片格式,比如jpeg和png,webp就不知道了,以后有机会试一试。

这个方法可以给视频和音频加封面。

上面给的例子是给视频加封面。

如果我把后面视频的格式变成m4a,结果发现效果只是单纯拷贝一个视频而已。

然后使用的过程中各种红色的找不到codec错误。

首先我对map命令不是很熟悉。这个场景就是map命令的一个应用。视频和图片作为输入,只取视频中的音频。所以我们就要定位到音频的位置。如果不用map,按照默认配置来的话,音频和视频和图片都会加入到新的文件里,可以看到video stream有两个,分别是视频和图片。

用map就是手动选择stream。

ffmpeg -i in.mp4 -i IMAGE -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic out.mp4

上面这个例子, map 0是第一个整个视频,map1则是第二个图片,和不用map的结果差不多。

估计,用map是为了后面的disposition选择对应的图片流更稳定。

在我的需求里,就不一样了。我的需求是把一个视频和封面合成成具有封面的音乐,这样等于删除了视频流。节省空间,封面只是起到美化作用而已。

所以我的map是下面这样的
ffmpeg -i input.flv -i input.png -map 0:a -map 1:v

map后面的第一个数字代表ffmpeg -i 输入的顺序,也就是程序实际读入stream的顺序。后面还可以定位,第一个是定位了stream文件,冒号后面的第二个则是定位具体的steam。可以用1,2之类的数字,用a,v则分别表示视频和音频,在只有一个视频和音频的情况下就等于选中一个特定的stream

再往后看,-c:v:1 png选项,指定第二个视频的处理方式是png。-c:v就是用来指定视频编码器,后面再跟的数字,就是指定具体stream。这里1指的是第二条stream流。第一条指的是第一个视频的视频流了。如果要指定第一个视频的视频流则是-c:v:0。这里我们通常不指定也可以,反正ffmpeg也会有一些自动化处理的。

-disposition 后面冒号跟的也是一个steam选择的功能。

-disposition:v:1 attached_pic

然后我的实际转化命令就变成

ffmpeg -i input.flv -i input.png -map 0:a -map 1:v  -disposition:v:1 attached_picpoert out.m4a

这样就能达成目的了。

经过测试opus格式也是不支持,估计也就只有mp3和aac这两种格式能支持吧?

加过封面后,用播放器加载能够正确使用封面了,并且mp3tag这类的工具也可以看到。

关于metadata的修改,网上发现一份很不错的文档,这里就放在下面了。方便查看。

首先修改metadata关键看标签名对应到ffmpeg里面是什么键值,wiki上有这样一张表

The following table shows the metadata keys that FFmpeg honors when muxing a QuickTime file. The low-level identifier column lists the atom name that the format uses to encode the data on disc, which is not interesting to most readers. For the interested but uninitiated, the notation, e.g., '\251nam' indicates a 4-byte code consisting of the byte A9 in hexadecimal (or 251 in octal) followed by the ASCII characters 'n', 'a', and 'm'.

Key iTunes field Low-level identifier
"title" Name '\251nam'
"author" Artist '\251ART'
"album_artist" Album Artist 'aART'
"album" Album '\251alb'
"grouping" Grouping '\251grp'
"composer" Composer '\251wrt'
"year" Year '\251day'
"track" Track Number 'trkn'
"comment" Comments '\251cmt'
"genre" Genre '\251gen'
"copyright" ?? '\251cpy'
"description" Description 'desc'
"synopsis" Information dialog when selecting "Show Description" in context menu 'ldes'
"show" Show 'tvsh'
"episode_id" Episode ID 'tven'
"network" ?? 'tvnn'
"lyrics" Lyrics '\251lyr'

How To: Create/Write ID3 tags using ffmpeg

Update: New article: How To: Dump and Load metadata with ffmpeg

I have received so many requests from my previous article, “ID3 tags on Windows using ffmpeg”, asking how to add, create or write ID3 tags with ffmpeg that I wrote this article in response.

The Basics

The option to write ‘tags’ to a file, known as metadata, is -metadata key=”value”. Here is a basic example:

  1. ffmpeg32 -i in.mp3 -metadata title="The Title You Want" out.mp3
    

You repeat the option once for each key/value pair you want to add to your file like this:

  1. ffmpeg32 -i in.mp3 -metadata title="The Title You Want" -metadata artist="Artist Name" -metadata album="Name of the Album" out.mp3
    

If you want to clear or delete a certain key/value pair, include the key but leave the value blank like this:

  1. ffmpeg32 -i out.mp3 -metadata title="The Title You Want" -metadata artist="" -metadata album="Name of the Album" out2.mp3
    

The examples shown above are the very basic. While they will work and will produce a file, there are more options that need to be included to achieve the desired results.

Adding, creating, deleting, or clearing ID3 tags in ffmpeg is very simple. The hard part is figuring out what items you want and what the ‘key’ names are. For example, in Windows you can right-click on the audio file, select ‘Properties’, then click on the ‘Details’ tab. There are several Property/Value pair items listed; “Title”, “Subtitle”, “Comments”, “Contributing artists”, “Album artist”, “Album”, etcetera. Similar information is available in iTunes by right-clicking on a name, choose ‘Get Info’, then click on the ‘Info’ tab. The following table may help you decide:

Windows iTunes (Info tab) id3v2.3 ffmpeg key ffmpeg example
Title Title TIT2 title -metadata title=”Adagio for Strings”
Subtitle Description (Video tab) TIT3 TIT3 -metadata TIT3=”Op. 91″
Rating n/a n/a n/a n/a
Comments Comments COMM n/a not implemented
Contributing artists Artist TPE1 artist -metadata artist=”Yo Yo Ma/London Symphony”
Album artist Album Artist TPE2 album_artist -metadata album_artist=”London Symphony”
Album Album TALB album -metadata album=”String Classics”
Year Year TYER date -metadata date=”2012″
# Track Number TRCK track -metadata track=”3/12″ (means track number 3 of 12)
Genre Genre TCON genre -metadata genre=”Classical”
Publisher n/a TPUB publisher -metadata publisher=”London Publishing”
Encoded by n/a TENC encoded_by -metadata encoded_by=”Telarc”
Author URL n/a WOAR n/a not implemented
Copyright (Not Editable) n/a TCOP copyright -metadata copyright=”â�� Telarc”
Composers n/a TCOM composer -metadata composer=”J.S. Bach”
Conductors n/a TPE3 performer -metadata performer=”T. S. Miles”
Group description Grouping TIT1 TIT1 -metadata TIT1=”The Classics”
Mood n/a n/a n/a n/a
Part of set Disc Number TPOS disc -metadata disc=”1/2″ (means disc number 1 of 2)
Initial key n/a TKEY TKEY -metadata TKEY=”G”
Beats-per-minute BPM TBPM TBPM -metadata TBPM=”120″
Part of a compilation Part of a compilation TCMP n/a not implemented
n/a n/a TLAN language -metadata language=”eng”
n/a n/a TSSE encoder -metadata encoder=”iTunes v10″

I suggest you experiment with the different keys to find what works best for you.

A little more advanced

Lets look at some more advanced options you can use to manipulate metadata. For example, if you want to clear or delete all the global metadata in a file. You could use the examples above and include the key and a blank value for each item but that could make your command line very long. Instead you can use -map_metadata -1 instead. Here it is in a command line:

  1. ffmpeg32 -i in.mp3 -map_metadata -1 out.mp3
    

Setting -map_metadata to -1 (negative one) tells ffmpeg to use a non-existent input thereby clearing or deleting all the global metadata.

You can get the metadatainformation for a media file by including a input file with no output file like this:

  1. ffmpeg32 -i in.mp3
    

FFmpeg is also able to dump metadata from media files into a simple UTF-8 encoded INI-like text file and then load it back using the metadata muxer/demuxer. Use a command line like the following to create the text file:

  1. ffmpeg32 -i in.mp3 -f ffmetadata metadata.txt
    

The file format is as follows:

  1. A file consists of a header and a number of metadata tags divided into sections, each on its own line.
  2. The header is a ‘;FFMETADATA’ string, followed by a version number (now 1).
  3. Metadata tags are of the form ‘key=value’.
  4. Immediately after the header follows global metadata.
  5. After global metadata there may be sections with per-stream/per-chapter metadata.
  6. A section starts with the section name in uppercase (i.e. STREAM or CHAPTER) in brackets (‘[’, ‘]’) and ends with a new/next section or end of file.
  7. At the beginning of a chapter section there may be an optional timebase to be used for start/end values. It must be in the form ‘TIMEBASE=num/den’, where num and den are integers. If the timebase is missing then start/end times are assumed to be in milliseconds. Next a chapter section must contain chapter start and end times in the form ‘START=num’, ‘END=num’, where num is a positive integer.
  8. Empty lines and lines starting with ‘;’ or ‘#’ are ignored.
  9. Metadata keys or values containing special characters (‘=’, ‘;’, ‘#’, ‘\’ and a newline) must be escaped with a backslash ‘\’.
  10. Note that whitespace in metadata (e.g. foo = bar) is considered to be a part of the tag (in the example above key is ‘foo ’, value is ‘ bar’).

A ffmetadata file might look like this:

;FFMETADATA1
title=bike\\shed
;this is a comment
artist=FFmpeg troll team

[CHAPTER]
TIMEBASE=1/1000
START=0
#chapter ends at 0:01:00
END=60000
title=chapter \#1
[STREAM]
title=multi\
line

You can edit the ffmetadata text file to include, change, or remove metadata by loading it into your media file.

To load the file and include the metadata in the output file, use -map_metadata like this:

  1. ffmpeg32 -i in.mp3 -i metadata.txt -map_metadata 1 -c:a copy -id3v2_version 3 -write_id3v1 1 out.mp3
    

The inputs -i in ffmpeg are counted zero-based. What that means is the first input from the command line example above -i in.mp3 is input number zero. The next input -i metadata.txt is input number one. So using -map_metadata 1 we are telling ffmpeg to map the metadata from input one, our text file, to the global metadata of the output file.

Remember the Version

If you are planning on using the media files with metadata on Windows, DO NOT forget to use these options in your command line:

-id3v2_version 3
-write_id3v1 1

As shown in Example 7 above.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,456评论 0 13
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,698评论 0 3
  • 我今天看了bachi这条狗狗的故事。 泪眼朦胧好几回,我的感触很简单:人世间太多情感我还没有经历! 不知道因为什么...
    弓子阅读 259评论 0 0
  • 文 若雨可岚 生命,简简单单的两个字,但它里面蕴含着多少深刻的哲理。生命,是一种希望的传承,是一种能量的传递,更...
    若雨可岚阅读 1,009评论 8 13
  • 恭喜你,属于她的他! 其实不是因为十月八上班 而是因为我看不得你为她披上婚纱 你会亲吻她说:“我愿意” 我怕心会疼...
    霍云瑶阅读 111评论 0 0