在 MP4 / MKV 上硬编码 FFMPEG 字幕

在 MP4 / MKV 上硬编码 FFMPEG 字幕

我正在使用 FFMPEG 将字幕硬编码到视频中。我读过很多关于最佳方法的帖子,以及是否使用 mkv 或 mp4,但仍然一无所知。下面的代码使用 mp4 文件。从输出来看,字幕似乎已处理,但 0kb 的大小似乎表明实际上没有渲染任何内容,输出文件有音频和视频,但没有字幕。

VLC 中的元数据显示字幕文件存在 - 但是为空。

下面是我的 SRT 文件的一个示例(如果在 1 中没有输入空行则会出现输入错误消息):

1
00:00:00,000 --> 00:00:00,001

2
00:00:00,001 –-> 00:00:04,761
He drives to school every morning

3
00:00:04,761 –-> 00:00:13,061
Cause love comes slow and it goes so fast

我的命令行代码是:

`ffmpeg -i #{inputfile} -i #{srt_file}  -filter_complex "[0:v][1:s]overlay" #{outputfile} -y`

我也尝试过:

`ffmpeg -i #{inputfile} -i #{srt_file}  -c:v libx264 -preset ultrafast \
-c:s mov_text -map 0 -map 1 #{outputfile}`

响应如下:

ffmpeg version 2.5.4 Copyright (c) 2000-2015 the FFmpeg developers
built on Feb 25 2015 15:52:19 with Apple LLVM version 6.0 (clang-600.0.54) 
(based on LLVM3.5svn)
configuration: --prefix=/usr/local/Cellar/ffmpeg/2.5.4 --enable-shared --enable-pthreads
--enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --
host-cflags= --host-ldflags= --enable-libx264 --enable-libmp3lame --enable-libvo-aacenc 
--enable-libxvid --enable-libass --enable-vda
libavutil      54. 15.100 / 54. 15.100
libavcodec     56. 13.100 / 56. 13.100
libavformat    56. 15.102 / 56. 15.102
libavdevice    56.  3.100 / 56.  3.100
libavfilter     5.  2.103 /  5.  2.103
libavresample   2.  1.  0 /  2.  1.  0
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  1.100 /  1.  1.100
libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 
'/Users/JW/Dropbox/T10/SBRI/_code/videos_final/3rd person present tense (303).mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf56.15.102
Duration: 00:00:43.17, start: 0.036281, bitrate: 862 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x406 [SAR 406:405 DAR 
16:9], 732 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
Metadata:
  handler_name    : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s    
(default)
Metadata:
  handler_name    : SoundHandler
Input #1, srt, from '/Users/JW/Dropbox/T10/SBRI/_code/subs/3rd person present tense (303)/srt_file.srt':
Duration: N/A, bitrate: N/A
Stream #1:0: Subtitle: subrip
[srt @ 0x7fd082005c00] sub2video: using 720x576 canvas
[libx264 @ 0x7fd082801200] using SAR=406/405
[libx264 @ 0x7fd082801200] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7fd082801200] profile High, level 3.0
[libx264 @ 0x7fd082801200] 264 - core 142 r2495 6a301b6 - H.264/MPEG-4 AVC codec - Copyleft     
2003-2014 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 
analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 
trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 
lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 
constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 
weightp=2 keyint=250 keyint_min=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf 
mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00

Output #0, mp4, to '/Users/JW/Dropbox/T10/SBRI/_code/videos_final/3rd person present tense 
(303)_subs.mp4':
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: isomiso2avc1mp41
encoder         : Lavf56.15.102
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 720x406 [SAR 406:405 
DAR 16:9], q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc (default)
Metadata:
  encoder         : Lavc56.13.100 libx264
Stream #0:1(und): Audio: aac (libvo_aacenc) ([64][0][0][0] / 0x0040), 44100 Hz, stereo, 
s16, 128 kb/s (default)
Metadata:
  handler_name    : SoundHandler
  encoder         : Lavc56.13.100 libvo_aacenc

Stream mapping:
Stream #0:0 (h264) -> overlay:main (graph 0)
Stream #1:0 (srt) -> overlay:overlay (graph 0)
overlay (graph 0) -> Stream #0:0 (libx264)
Stream #0:1 -> #0:1 (aac (native) -> aac (libvo_aacenc))

Press [q] to stop, [?] for help
sub2video: non-bitmap subtitle
frame= 1029 fps= 71 q=-1.0 Lsize=    4319kB time=00:00:43.17 bitrate= 819.6kbits/s
video:3612kB audio:675kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:     
0.751962%
[libx264 @ 0x7fd082801200] frame I:44    Avg QP:17.59  size: 16172
[libx264 @ 0x7fd082801200] frame P:495   Avg QP:22.62  size:  5016
[libx264 @ 0x7fd082801200] frame B:490   Avg QP:23.28  size:  1027
[libx264 @ 0x7fd082801200] consecutive B-frames: 24.2% 34.4%  7.6% 33.8%
[libx264 @ 0x7fd082801200] mb I  I16..4: 27.2% 43.8% 28.9%
[libx264 @ 0x7fd082801200] mb P  I16..4:  5.9% 14.8%  2.9%  P16..4: 31.1% 10.7%  4.3%  0.0%  
0.0%    skip:30.3%
[libx264 @ 0x7fd082801200] mb B  I16..4:  0.2%  0.5%  0.2%  B16..8: 26.2%  3.3%  0.6%   
direct: 1.1%  skip:67.8%  L0:40.7% L1:52.4% BI: 6.9%
[libx264 @ 0x7fd082801200] 8x8 transform intra:57.6% inter:72.8%
[libx264 @ 0x7fd082801200] coded y,uvDC,uvAC intra: 41.3% 60.2% 22.0% inter: 9.1% 14.5% 0.6%
[libx264 @ 0x7fd082801200] i16 v,h,dc,p: 41% 25% 11% 22%
[libx264 @ 0x7fd082801200] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 20% 23%  4%  5%  6%  5%  5%  4%
[libx264 @ 0x7fd082801200] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 29% 20% 12%  5%  8%  9%  7%  6%  4%
[libx264 @ 0x7fd082801200] i8c dc,h,v,p: 53% 20% 21%  6%
[libx264 @ 0x7fd082801200] Weighted P-Frames: Y:14.1% UV:9.9%
[libx264 @ 0x7fd082801200] ref P L0: 68.6% 14.5% 12.3%  4.2%  0.4%
[libx264 @ 0x7fd082801200] ref B L0: 91.7%  7.5%  0.8%
[libx264 @ 0x7fd082801200] ref B L1: 98.4%  1.6%
[libx264 @ 0x7fd082801200] kb/s:689.28

我究竟做错了什么?

答案1

sub2video:非位图字幕

这是你的错误。你有基于文本的字幕,所以你需要用subtitles筛选

ffmpeg -i input.mp4 -filter:v subtitles=subtitles.srt output.mp4

覆盖过滤器仅当字幕是图像时才有效,例如 dvdsub 的情况。

此外,问题似乎是您的字幕文件格式不正确。您的时间戳定义包含一个短划线 ( ),而不是正常的减号连字符 ( -):

–->

用正确的减号连字符替换它:

-->

答案2

您需要一个字体配置文件,即:

C:\WINDOWS\字体

单声道等宽屏

无衬线 衬线 等宽无衬线

Times Times New Roman 衬线体 Helvetica Arial sans Courier Courier New 等宽衬线体 Times New Roman sans Arial 等宽体 Andale Mono Courier New 等宽体 Courier 等宽体

将其保存为 ffmpeg bin 目录中的 fonts.xml,并在批处理/cml 中引用它

设置 FONTCONFIG_FILE=fonts.xml 设置 FONTCONFIG_PATH=C:\Program Files\ffmpeg\bin

相关内容