简体   繁体   English

如何在 C++ 中使用 ffmpeg 提取音频形式的视频?

[英]How to extract audio form video using ffmpeg in C++?

I'm developing a C++ app that uses FFmpeg to play audio/video.我正在开发一个使用 FFmpeg 播放音频/视频的 C++ 应用程序。 Now I want to enhance the application to allow the users to extract audio from video.现在我想增强应用程序以允许用户从视频中提取音频。 How can FFmpeg can be used for this? FFmpeg 如何用于此目的? I searched a lot about this but I was not able to find a tutorial regarding it.我搜索了很多关于这个,但我找不到关于它的教程。

You need to你需要

  1. Open the input context [ avformat_open_input ]打开输入上下文 [ avformat_open_input ]
  2. Get the stream information [ avformat_find_stream_info ]获取流信息 [ avformat_find_stream_info ]
  3. Get the audio stream:获取音频流:
if (inputFormatContext->streams[index]->codec->codec_type ==
    AVMEDIA_TYPE_AUDIO) {
  inputAudioStream = inputFormatContext->streams[index];
}
  1. Read each packet.读取每个数据包。 AVPacket packet; AVPacket 数据包;
int ret = av_read_frame(inputFormatContext, &packet);
if (ret == 0) {
  if (packet.stream_index == inputAudioStream->index) {
    // packet.data will have encoded audio data.
  }
}

This seems like a simple scripting task... why do you want to use the heavy artillery (C/C++) to swat a fly?这似乎是一个简单的脚本任务……为什么要使用重炮(C/C++)来打苍蝇?

I use Applescript to build/run an ffmpeg command line via a Bash shell.我使用 Applescript 通过 Bash shell 构建/运行 ffmpeg 命令行。 The only reason I involve Applescript is so I can invoke it as a droplet (ie drag-and-drop the file(s) onto the app and have it run without interaction.)我涉及 Applescript 的唯一原因是我可以将它作为一个 droplet 调用(即将文件拖放到应用程序上并让它在没有交互的情况下运行。)

I get that you're probably on Windows, meaning no Applescript and no Bash.我知道您可能使用的是 Windows,这意味着没有 Applescript 和 Bash。 But surely something lighter than C can build/run an ffmpeg command line.但是肯定比 C 更轻的东西可以构建/运行 ffmpeg 命令行。 It's really as simple as:这真的很简单:

ffmpeg -i infile.mp4 -b 160k outfile.mp3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM