ffmpeg 一条命令剪辑视频并添加字幕

huangapple go评论82阅读模式
英文:

ffmpeg cut video and burn subtitle in a single command

问题

我想从视频中剪切一部分并在该部分中添加字幕。

我可以通过以下3个步骤来完成:

  1. 剪切视频
    ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -c copy out.mp4

  2. 剪切字幕
    ffmpeg -i sub.srt -ss 25:00 -to 26:00 out.srt

  3. 将字幕烧入视频片段
    ffmpeg -i out.mp4 -vf subtitles=out.srt -c:a copy -y final.mp4

但我想要在单个ffmpeg命令中完成这个操作。

如果我这样做
ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -vf subtitles=sub.srt -c:a copy -y final.mp4
视频被剪切,但没有字幕被烧入。
这很快。

如果我这样做
ffmpeg -i vid.mp4 -ss 25:00 -to 26:00 -vf subtitles=sub.srt -c:a copy -y final.mp4
视频被剪切,字幕被正确烧入,但在开始写final.mp4时存在延迟。
我认为ffmpeg正在处理vid.mp4,从一开始处理直到达到-ss时间(并丢弃那部分),然后继续处理并写入final.mp4。

有没有一种快速且在单个ffmpeg命令中完成此操作的方法?
就像ffmpeg直接跳到-ss时间并剪切,处理它,在其中烧入字幕。

谢谢。

英文:

I want to cut a piece out of a video and burn subtitle in that piece.

I can do this in 3 steps:

  1. cut the video
    ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -c copy out.mp4

  2. cut the subtitle
    ffmpeg -i sub.srt -ss 25:00 -to 26:00 out.srt

  3. burn subtitle in the video piece
    ffmpeg -i out.mp4 -vf subtitles=out.srt -c:a copy -y final.mp4

But I want to do this in a single ffmpeg command.

If I do this
ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -vf subtitles=sub.srt -c:a copy -y final.mp4
the video is cut but no subtitle is burned into it.
This is fast.

If I do this
ffmpeg -i vid.mp4 -ss 25:00 -to 26:00 -vf subtitles=sub.srt -c:a copy -y final.mp4
the video is cut and subtitles burned correctly,but there is a delay in starting writing the final.mp4.
I think ffmpeg is processing vid.mp4 from the beginning till it reach the -ss time (and drop that part)
then continue processing and writing it to final.mp4

Is there a way to do this fast and in a single ffmpeg command?
Like ffmpeg going directly to -ss time and cut that, process it, burn subtitle in it.

Thanks

答案1

得分: 11

是的,字幕过滤器会在内部打开字幕文件,并不知道视频的查找点,因为ffmpeg在查找后会重置时间戳。

因此要保留源时间戳,然后重置它们,即:

ffmpeg -ss 25:00 -to 26:00 -copyts -i vid.mp4 -vf subtitles=sub.srt -c:a copy -ss 25:00 -y final.mp4
英文:

Yes, the subtitles filter opens the subtitle file internally and isn't aware of the seek point for the video since ffmpeg resets timestamps after seeking.

So preserve the source timestamps and then reset them i.e.

ffmpeg -ss 25:00 -to 26:00 -copyts -i vid.mp4 -vf subtitles=sub.srt  -c:a copy -ss 25:00 -y final.mp4

答案2

得分: 0

字幕作为单独的流传输(就像视频和音频一样)。您需要指定要复制的流。假设视频是流 #0:0,音频是流 #0:1,字幕是流 #0:2,命令将是

ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -c copy -map 0:0 -map 0:1 -map 0:2 out.mp4

您可以使用 ffprobe 查询现有流:

ffprobe -i vid.mp4

这将产生类似以下的结果:

Stream #0:0(eng): Video: h264 (High), yuv420p(progressive), 1912x1036 [SAR 1:1 DAR 478:259], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
Stream #0:1(fre): Audio: ac3, 48000 Hz, 5.1(side), fltp, 448
Stream #0:2(eng): Subtitle: subrip
英文:

Subtitles go as a separate stream (just as video and audio do). You need to specify which streams you want to get copied. Assuming video is Stream #0:0, audio is Stream #0:1 and subtitles are Stream #0:2, the command will be

ffmpeg -ss 25:00 -to 26:00 -i vid.mp4 -c copy -map 0:0 -map 0:1 -map 0:2 out.mp4

You can inquire about existing streams using ffprobe:

ffprobe -i vid.mp4

which will yield something like

Stream #0:0(eng): Video: h264 (High), yuv420p(progressive), 1912x1036 [SAR 1:1 DAR 478:259], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
Stream #0:1(fre): Audio: ac3, 48000 Hz, 5.1(side), fltp, 448
Stream #0:2(eng): Subtitle: subrip

huangapple
  • 本文由 发表于 2020年1月3日 16:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定