使用 ffmpeg 给视频添加字幕失败

使用 ffmpeg 给视频添加字幕失败

我正在尝试使用 ffmpeg 和文件为视频添加字幕.srt。使用以下命令:

ffmpeg -i videopath.mp4 -i subtitlefilepath.srt -c:v libx264 -preset ultrafast -ar 44100 -ac 2 -ab 128k -strict -2 videooutput.mp4

.srt文件看起来像

1
00:00:00,30-->00:00:05,30
Sample sub title for video 

后面的 30 是什么duration

输出:

ffmpeg version git-2013-12-11-3b28564 Copyright (c) 2000-2014 the FFmpeg developers  built on Jan 22 2014 13:22:00 with gcc 4.6 (GCC) 20120106 (peelease)  configuration: --arch=arm --target-os=linux --enable-cross-compile --cross-prefix=/Users/sunitha/Downloads/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86/bin/arm-linux-androideabi- --prefix=../build --sysroot=/Users/sunitha/Downloads/android-ndk-r9c/platforms/android-9/arch-arm --extra-cflags=' -I/Users/sunitha/Downloads/android-ndk-r9c/platforms/android-9/arch-arm/usr/include -I../libmp3lame/include -I../libx264/include -DANDROID -marm -march=armv6 ' --extra-ldflags=' -L/Users/sunitha/Downloads/android-ndk-r9c/platforms/android-9/arch-arm/usr/lib -L../libmp3lame/lib -L../libx264/lib' --disable-debug --enable-libmp3lame --enable-libx264 --disable-ffplay --disable-ffprobe --disable-ffserver --enable-avfilter --enable-decoders --enable-demuxers --enable-encoders --enable-filters --enable-indevs --enable-network --enable-parsers --enable-protocols --enable-swscale --enable-gpl --enable-nonfree
libavutil      52. 63.100 / 52. 63.100  
libavcodec     55. 48.102 / 55. 48.102  
libavformat    55. 25.101 / 55. 25.101  
libavdevice    55.  5.102 / 55.  5.102  
libavfilter     4.  1.100 /  4.  1.100  
libswscale      2.  5.101 /  2.  5.101  
libswresample   0. 17.104 /  0. 17.104  
libpostproc    52.  3.100 / 52.  3.100

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DCIM/Camera/20141022_125926.mp4':  
    Metadata:    
        major_brand     : isom    
        minor_version   : 0    
        compatible_brands: isom3gp4    
        creation_time   : 2014-10-22 07:29:39  
        Duration: 00:00:08.58, start: 0.000000, bitrate: 11752 kb/s  
    Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p(tv, smpte170m), 1280x720, 11758 kb/s, 29.33 fps, 30 tbr, 90k tbn, 180k tbc (default)    
    Metadata:      
        rotate          : 90      
        creation_time   : 2014-10-22 07:29:39      
        handler_name    : VideoHandle    
    Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, mono, fltp, 60 kb/s (default)    
    Metadata:      
        creation_time   : 2014-10-22 07:29:39      
        handler_name    : SoundHandle

Input #1, srt, from '/storage/emulated/0/DCIM/Camera/subtitle.srt':  Duration: N/A, bitrate: N/A    
Stream #1:0: Subtitle: subrip

[libx264 @ 0x11cf050] using cpu capabilities: ARMv6 NEON[libx264 @ 0x11cf050] profile Constrained Baseline, level 3.1
[libx264 @ 0x11cf050] 264 - core 140 r2377 1ca7bb9 - H.264/MPEG-4 AVC codec - Copyleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=1 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0

Output #0, mp4, to '/storage/emulated/0/VideoOverLay/videooverlay1413965066659.mp4':  
    Metadata:    
        major_brand     : isom    
        minor_version   : 0    
        compatible_brands: isom3gp4    
        encoder         : Lavf55.25.101    
    Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=-1--1, 15360 tbn, 30 tbc (default)    
    Metadata:      
        rotate          : 90      
        creation_time   : 2014-10-22 07:29:39      
        handler_name    : VideoHandle    
    Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s (default)    
    Metadata:      
        creation_time   : 2014-10-22 07:29:39      
        handler_name    : SoundHandle

Stream mapping:  
    Stream #0:0 -> #0:0 (h264 -> libx264)  
    Stream #0:1 -> #0:1 (aac -> aac)

Press [q] to stop, [?] for help
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.47 bitrate=   0.8kbits/s    
frame=    0 fps=0.0 q=0.0 size=       0kB time=00:00:00.91 bitrate=   0.4kbits/s    
frame=    3 fps=1.2 q=23.0 size=      38kB time=00:00:01.00 bitrate= 311.9kbits/s    
frame=    6 fps=1.9 q=22.0 size=      7
10-22 13:35:08.365: D/dalvikvm(1754): GC_CONCURRENT freed 1968K, 26% free 16486K/22120K, paused 14ms+6ms, total 59ms
10-22 13:35:08.365: I/exit code >>>>>>>>..(1754): 0
10-22 13:35:08.365: I/exit code >>>>>>>>..(1754): 0

我看不到视频上的字幕。问题出在哪里?

答案1

您没有告诉 ffmpeg 使用字幕。如果有多个输入,ffmpeg 只会映射一个,IIRC:

Stream mapping:  
    Stream #0:0 -> #0:0 (h264 -> libx264)  
    Stream #0:1 -> #0:1 (aac -> aac)

因此,尝试如下方法:

ffmpeg -i videopath.mp4 -i subtitlefilepath.srt \
-c:v libx264 -preset ultrafast \
-ar 44100 -ac 2 -ab 128k -strict -2 \
-c:s mov_text -map 0 -map 1 \
videooutput.mp4

这将按原样复制字幕,并将输入视频 ( -map 0) 和字幕文件 ( -map 1) 映射到输出。您应该看到类似以下内容:

Stream mapping:  
    Stream #0:0 -> #0:0 (h264 -> libx264)  
    Stream #0:1 -> #0:1 (aac -> aac)
    Stream #1:0 -> #1:0 (srt -> mov_text)

您还可以使用subtitles筛选对 subs 进行硬编码。为此,您需要编译 ffmpeg 版本libass,而您使用的版本不需要。

相关内容