使用 ffmpeg 从视频中提取每个音频和字幕 [英] Extract every audio and subtitles from a video with ffmpeg

查看:377
本文介绍了使用 ffmpeg 从视频中提取每个音频和字幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个音轨和字幕要提取到一个 .mkv 文件中.我是 ffmpeg 命令的新手,这是我尝试过的(音频):

I have multiple audio tracks and subtitles to extract in a single .mkv file. I'm new to ffmpeg commands, this is what I've tried (audio):

ffmpeg -i VIDEO.mkv -vn -acodec copy AUDIO.aac

它只提取了 1 个音频.我想要的是告诉 ffmpeg 将每个音频文件和字幕文件提取到目的地,并保留每个文件和扩展名的原始名称.(因为我不知道音频文件是哪个扩展名,有时可能是 .flac.aac).

我不确定我在网上找到的解决方案,因为它非常复杂,我需要解释一下它是如何工作的,以便我将来可以操纵命令.顺便说一下,我计划从 Windows CMD 运行代码.

谢谢.

It just extract 1 audio. What I want is tell ffmpeg to extract every single audio files and subtitle files to a destination, and keep the original name of each files and extensions. (Because I don't know which extension does the audio files are, sometimes maybe .flac or .aac).

I'm not sure about the solutions I'd found online, because it's quite complicated, and I need explanations to know how it's works, so that I can manipulate the command in the future. By the way, I planned to run the code from Windows CMD.

Thanks.

推荐答案

ffmpeg 中还没有选项可以自动将所有流提取到适当的容器中,但当然可以手动进行.

There is no option yet in ffmpeg to automatically extract all streams into an appropriate container, but it is certainly possible to do manually.

您只需要知道要提取的格式的适当容器即可.

You only need to know the appropriate containers for the formats you want to extract.

默认 流选择 只为每个流类型选择一个流,因此您必须使用 -map 选项手动映射每个流.

Default stream selection only chooses one stream per stream type, so you have to manually map each stream with the -map option.

使用 ffmpegffprobe 您可以获得每个流中的信息,并且有一个 多种格式(xml、json、cvs 等)可满足您的需求.

Using ffmpeg or ffprobe you can get the info in each individual stream, and there is a wide variety of formats (xml, json, cvs, etc) available to fit your needs.

ffmpeg -i input.mkv

结果输出(我删掉了一些额外的东西,流号和格式信息很重要):

The resulting output (I cut out some extra stuff, the stream numbers and format info are what is important):

Input #0, matroska,webm, from 'input.mkv':
  Metadata:
  Duration: 00:00:05.00, start: 0.000000, bitrate: 106 kb/s
    Stream #0:0: Video: h264 (High 4:4:4 Predictive), yuv444p, 320x240 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
    Stream #0:1: Audio: vorbis, 44100 Hz, mono, fltp (default)
    Stream #0:2: Audio: aac, 44100 Hz, mono, fltp (default)
    Stream #0:3: Audio: flac, 44100 Hz, mono, fltp (default)
    Stream #0:4: Subtitle: ass (default)

ffprobe 示例

ffprobe -v error -show_entries stream=index,codec_name,codec_type input.mkv

结果输出:

[STREAM]
index=0
codec_name=h264
codec_type=video
[/STREAM]
[STREAM]
index=1
codec_name=vorbis
codec_type=audio
[/STREAM]
[STREAM]
index=2
codec_name=aac
codec_type=audio
[/STREAM]
[STREAM]
index=3
codec_name=flac
codec_type=audio
[/STREAM]
[STREAM]
index=4
codec_name=ass
codec_type=subtitle
[/STREAM]

2.提取流

使用来自上述命令之一的信息:

2. Extract the streams

Using the info from one of the commands above:

ffmpeg -i input.mkv \
-map 0:v -c copy video_h264.mkv \
-map 0:a:0 -c copy audio0_vorbis.oga \
-map 0:a:1 -c copy audio1_aac.m4a \
-map 0:a:2 -c copy audio2.flac \
-map 0:s -c copy subtitles.ass

在这种情况下,上面的例子与:

In this case, the example above is the same as:

ffmpeg -i input.mkv \
-map 0:0 -c copy video_h264.mkv \
-map 0:1 -c copy audio0_vorbis.oga \
-map 0:2 -c copy audio1_aac.m4a \
-map 0:3 -c copy audio2.flac \
-map 0:4 -c copy subtitles.ass

  • 我更喜欢第一个例子,因为 input file index:stream specifier:stream index 更灵活和高效;它也不太容易出现错误映射.

    • I prefer the first example because the input file index:stream specifier:stream index is more flexible and efficient; it is also less prone to incorrect mapping.

      查看关于流说明符-map option 以完全理解语法.FFmpeg mux 视频和音频(来自另一个视频) - 映射问题的答案中提供了其他信息.

      See documentation on stream specifiers and the -map option to fully understand the syntax. Additional info is in the answer to FFmpeg mux video and audio (from another video) - mapping issue.

      这些示例将流复制(重新复用)所以没有将发生重新编码.

      These examples will stream copy (re-mux) so no re-encoding will occur.

      将流与某些常见格式的输出扩展名匹配的部分列表:

      A partial list to match the stream with the output extension for some common formats:

      <头>
      视频格式扩展
      H.264.mp4、.m4v、.mov、.h264、.264
      H.265/HEVC.mp4、.h265、.265
      VP8/VP9.webm
      AV1.mp4
      MPEG-4.mp4、.avi
      MPEG-2.mpg、.vob、.ts
      DV.dv、.avi、.mov
      理论.ogv/.ogg
      FFV1.mkv
      几乎所有.mkv, .nut

      <头>
      音频格式扩展
      AAC.m4a、.aac
      MP3.mp3
      PCM.wav
      Vorbis.oga/.ogg
      作品.opus、.oga/.ogg、.mp4
      FLAC.flac、.oga/.ogg
      几乎所有.mka, .nut

      <头>
      字幕格式扩展
      Subrip/SRT.srt
      变电站Alpha/ASS.ass

      这篇关于使用 ffmpeg 从视频中提取每个音频和字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆