ffmpeg subtitle example

example 1.

 

 在ffmpeg 加入libass时, 如果找不到libass, 可在脚本中加入

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/ass/lib/pkgconfig

 

./ffmpeg -f mpegts -i ../film1.ts -an -c:v libx264 -vf "subtitles=tt.srt" ff.ts

 

./ffmpeg -f mpegts -i ../film.v -an -c:v libx264 -s 720x480 -b:v 1000k -vf "ass=tt.ass" ass.ts

 

记得将下述文本保存为utf-8格式

1
00:00:00,000 --> 00:00:01,284
《吸血鬼日记》前情提要
Previously on "the vampire diaries"...

2
00:00:01,335 --> 00:00:02,835
吸血鬼和狼人过去可以自由地出没
Vampires and werewolves used to roam freely

3
00:00:02,920 --> 00:00:05,839
直到一个巫师对他们施加了诅咒来限制他们的能力
Until a shaman put a curse on them to limit their power.

4
00:00:05,840 --> 00:00:08,091
从那以后,狼人们只能在满月时变身
Since then, werewolves can only turn on a full moon

5
00:00:08,142 --> 00:00:09,509
而吸血鬼不能见日光
And vampires are weakened by the sun.

6
00:00:09,594 --> 00:00:12,562
狼人的诅咒封印在月光石中
The werewolf part of the curse, it's sealed with a moonstone.

7
00:00:12,630 --> 00:00:14,464
他跟我说了你的前女友 Katherine
So tell me about your ex, Katherine?

8
00:00:14,515 --> 00:00:15,465
你一定是Elena
You must be Elena.

9
00:00:15,516 --> 00:00:17,267
我们怎么会长得一模一样
How do we look exactly alike?

 

 

2. tt.ass file

 

[Script Info]


[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: *Default,微软雅黑,21,&H00FFFFFF,&H0000FFFF,&H2D804000,&H32000000,-1,0,0,0,100,100,0,0,0,2,1,2,5,5,5,134
Style: logo,微软雅黑,21,&H00FFFFFF,&HF0000000,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,1,2,5,5,5,134

[Events]
Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:00.03,0:00:01.55,*Default,NTP,0000,0000,0000,,吸血鬼日记前情提要\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}Previously on "the Vampire Diaries"...
Dialogue: 0,0:00:01.58,0:00:04.51,*Default,NTP,0000,0000,0000,,Elena很享受你们兄弟俩对她的爱慕吧\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}Does Elena enjoy having both of you worship at her altar?
Dialogue: 0,0:00:04.51,0:00:05.44,*Default,NTP,0000,0000,0000,,我听说过你\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}I've heard about you,
Dialogue: 0,0:00:05.46,0:00:07.72,*Default,NTP,0000,0000,0000,,疯狂又冲动的吸血鬼\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}the crazy,impulsive vampire
Dialogue: 0,0:00:07.76,0:00:09.24,*Default,NTP,0000,0000,0000,,爱上了弟弟的女朋友\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}in love with his brother's girl.
Dialogue: 0,0:00:09.27,0:00:10.11,*Default,NTP,0000,0000,0000,,Stefan在哪儿\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}Where's Stefan?
Dialogue: 0,0:00:10.13,0:00:12.11,*Default,NTP,0000,0000,0000,,为了救他哥哥 他把自己献给了Klaus\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}He gave himself over to Klaus to save his brother.
Dialogue: 0,0:00:12.14,0:00:14.77,*Default,NTP,0000,0000,0000,,Klaus是有狼人血统的吸血鬼\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}Klaus is a vampire born of a werewolf bloodline.
Dialogue: 0,0:00:14.77,0:00:16.89,*Default,NTP,0000,0000,0000,,诅咒使他体内的狼人特征无法显现出来\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}The curse has kept his werewolf aspect from manifesting,
Dialogue: 0,0:00:16.90,0:00:19.10,*Default,NTP,0000,0000,0000,,一旦他打破诅咒 他就变成了彻底的混血\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}but if he breaks it, he'll be a true hybrid.
Dialogue: 0,0:00:19.13,0:00:21.41,*Default,NTP,0000,0000,0000,,我要创造更多像我这样的\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}I want to create more of me.
Dialogue: 0,0:00:21.44,0:00:23.72,*Default,NTP,0000,0000,0000,,你可是我这么久来见到的第一个狼人\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}Now, you being the first werewolf I've come across,
Dialogue: 0,0:00:23.75,0:00:26.14,*Default,NTP,0000,0000,0000,,我要你带我去你的部落\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}I need you to direct me to your pack.
Dialogue: 0,0:00:26.18,0:00:27.24,*Default,NTP,0000,0000,0000,,你知道我死过 对吧\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}You know I died, right,
Dialogue: 0,0:00:27.27,0:00:29.72,*Default,NTP,0000,0000,0000,,然后Bonnie用魔力让我起死回生\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}and Bonnie used magic to bring me back to life?
Dialogue: 0,0:00:29.78,0:00:32.95,*Default,NTP,0000,0000,0000,,自从我复活后 我总能看到些不该看到的东西\N{\1c&HFFFFFF&}{\3a&H82&\4c&H030303&}{\fnArial Black}{\fs20}{\b1}{\fe0}{\shad1}{\3c&H030303&}{\4c&H030303&}Ever since I came back, I've been seeing things I shouldn't be seeing.

 

 ===================================================================================================


Subtitle Background[edit]

Subtitles may come either from a separate text file (typically with a .srt extension) or from a dedicated stream within the video file. In the latter case, the subtitles are typically stored not as text but as bitmaps that are overlayed on the video and may be referred to as "VOBSUBs" for DVDs or "PGS" for Bluray.

Note that a subtitle stream need not be present for the entirety of the video and may first appear only when the first subtitled dialog occurs. Thus, the subtitle stream may not be detected and listed by:

 ffmpeg -i <input>

which only searches a short distance into the input file.

To coerce ffmpeg to search further for the subtitle stream, use options:

 -probesize <bytes> -analyzeduration <microseconds>

which will cause ffmpeg to search until the first of those limits is reached. Note that both of these options must appear on the command line before the specification of the input via -i. For example:

 ffmpeg -probesize 50M -analyzeduration 100M -i vts.vob

will search through vts.vob for all streams until it has read 50 MB of data or 100 seconds of video, whichever comes first.


Outputting Subtitles[edit]

Subtitles may be included in an encoded video file as a parallel stream, or may be rendered permanently (aka "burned") into the video itself.


Encoding Subtitles to an Output Stream[edit]

ffmpeg -i "INPUT" [-sub_charenc [character encoding]] -i "SUBTITLE_INPUT" 
        -vcodec [codec] -acodec [codec] -scodec [codec] [...] "OUTPUT"

Set Subtitles Character Encoding Conversion[edit]

Set input text subtitles character encoding conversion

-sub_charenc [character encoding]

If you want to import and encode a subtitle plaintext file (Windows/ANSI) which contains special characters of some languages (like French, German, Italian, Spanish and so on) this option is needed for subtitle encoding conversion from Windows/ANSI to UTF-8.

[character encoding] parameter examples:
CP1252, CP874, CP936, CP1250, ... , CP1258,
LATIN1 , LATIN2 , ... , LATIN8, LATIN-9, LATIN10,
ISO-8859-1, ISO-8859-2, ... , ISO-8859-16,
UTF-8, UTF-16, UTF-32
UNICODEBIG, UNICODELITTLE

See also: sub_charenc_parameters.txt in #2431

Notes:
[-sub_charenc [character encoding]] is not mandatory, but whenever used, it must be placed right before the reference of the subtitle to be processed with that encoding.
If you try to import ANSI subtitle files with a newer version of FFmpeg without using of "-sub_charenc" you can get an error message like:
Invalid UTF-8 in decoded subtitles text; maybe missing -sub_charenc option
Then you have to use option "-sub_charenc" to define the right coding format or code page of subtitle file.

Set Subtitle Codec[edit]

Force to set subtitle codec ('copy' to copy stream)

-scodec [subtitle codec]

Not every subtitle codec can be used for every video container format!

[subtitle codec] parameter examples:
for MKV containers: copy, ass, srt, ssa
for MP4 containers: copy, mov_text
for MOV containers: copy, mov_text

Note:
Many Philips Blu-Ray Players, Samsung Smart TV's and other standalone players can only read "SRT" subtitle streams in "MKV" files.

Set Subtitle Language[edit]

Set subtitle language using the ISO 639 language code (3 letters)

-metadata:s:s:[stream number] language=[language code]

-metadata:s:s:0 ... that means to set the metadata for ... Stream:Subtitle:Number of stream, starting with 0

[language code] parameter examples:
eng - English
fre or fra - French
ger - German
ita - Italian
spa - Spanish

FFmpeg command-line examples to importing subtitle files:[edit]

Import a subtitle file (copy video/audio streams, without re-encoding):[edit]
ffmpeg -i input.mp4 -sub_charenc CP1252 -i subtitle.srt -map 0:v -map 0:a -c copy -map 1 -c:s:0 mov_text -metadata:s:s:0 language=ger output.mp4
ffmpeg -i input.mkv -sub_charenc CP1252 -i subtitle.srt -map 0:v -map 0:a -c copy -map 1 -c:s:0 srt -metadata:s:s:0 language=fre output.mkv

or

ffmpeg -i input.mp4 -sub_charenc CP1252 -i subtitle.srt -vcodec copy -acodec copy -scodec mov_text -metadata:s:s:0 language=ger output.mp4
ffmpeg -i input.mkv -sub_charenc CP1252 -i subtitle.srt -vcodec copy -acodec copy -scodec srt -metadata:s:s:0 language=fre output.mkv
Import subtitle and re-encode video/audio streams:[edit]
ffmpeg -i input.mov -sub_charenc WINDOWS-1252 -i subtitle.srt -vcodec h264 -acodec mp3 -scodec mov_text -metadata:s:s:0 language=spa output2.mp4
ffmpeg -i input.avi -sub_charenc ISO-8859-1 -i subtitle.srt -vcodec h264 -acodec ac3 -scodec srt -metadata:s:s:0 language=ita output2.mkv

Rendering Subtitles into the Video[edit]

Subtitles may be rendered permanently into the video stream by specifying a complex filter on the command line:

 -filter_complex "[0:v][0:s]overlay"

This can be interpreted as "take all video streams from the first listed input, then all subtitle streams from the first listed input, and overlay the latter on the former to produce a single video stream that replaces all consumed streams". The new stream will be mapped by the encoder as any video input stream normally would.

Subtitle Color[edit]

For VOBSUBs (ie. bitmapped subtitles on DVDs), the color of the subtitles is determined by a palette that is specified in a .IFO file on the DVD, but this is not available to ffmpeg and thus the colors of subtitles will be largely arbitrary unless the palette is specified on the command line with:

 -palette "<list of 16 comma-separated RRGGBB hex codes>"

Unfortunately, the palette entries used to render the subtitles vary from one video to the next, so some experimentation is required. Typically one entry will determine the font color and another entry will determine the outline color, though some videos have subtitles that use multiple outlines, each with its own palette entry.

Example[edit]

The full command line to search relatively deeply for a subtitle stream and then render it in green on black into an h264 output video might look as follows:

 ffmpeg -probesize 100M -analyzeduration 120M -palette "ffffff,000000,ffffff,00ff00,ffffff,ffffff,ffffff,ffffff,ffffff,ffffff,ffffff,ffffff,ffffff,ffffff,ffffff,ffffff" -i input.vob -filter_complex "[0:v][0:s]overlay" -acodec copy -vcodec libx264 output.mp4

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值