复制avcodec参数 [英] Copying avcodec parameters

查看:101
本文介绍了复制avcodec参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用libav将MP4文件转换为MP3文件.基本上试图实现ffmpeg -i filename.mp4 filename.mp3的作用.我找到了官方示例.但是,当我使用输入MP4和输出MP3运行它时,出现错误:

I am trying to use libav to convert an MP4 file to an MP3 file. Basically trying to achieve what ffmpeg -i filename.mp4 filename.mp3 does. I've found this official example. But when I run it with an input MP4 and an output MP3 I get an error:

无效的音频流.只需一个MP3音频流.

Invalid audio stream. Exactly one MP3 audio stream is required.

我对这个库一点都不熟悉,但是我认为我已将问题缩小到

I am not at all familiar with this library but I think I have narrowed the problem down to this line:

ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);

似乎复制了一个视频文件的所有流,但是我们只需要一个MP3文件?我不知道.似乎没有一个功能可以仅复制与音频相关的参数.我检查了源代码,avcodec_parameters_copy做一个简单的memcpy.

It seems to copy all streams for a video file but we only need one for the MP3 file? I am not sure. There doesn't seem to be a function to copy only the parameters relevant to audio. I checked the sources, avcodec_parameters_copy does a simple memcpy.

问题:

  1. 这是实际问题吗?
  2. 我该如何解决?
  3. 我是否在正确的轨道上实现了从视频文件中提取音频的目标?我见过这个问题(以及其他类似的问题,例如),但似乎没有一个完整的代码示例.也缺少该库的C API文档.
  1. Is this the actual problem?
  2. How do I solve it?
  3. Am I on the right track to achieve the goal of extracting audio from a video file? I've seen this question (and other similar questions like this and this) on here but none seem to have a complete code example. The C API documentation for this library is also a little lacking.

推荐答案

在mp4文件中可以有多个音轨,但在mp3文件中只能有一个这样的音轨.对于 remuxing 示例,最简单的解决方法是:替换第101-103行:

You can have multiple audio tracks in mp4 file, but only one such track in an mp3 file. The easiest fix for the remuxing example would be to replace lines 101-103:

if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
    stream_index != 0) {

自然,仅当输出为mp3时才有意义.

This, naturally, is relevant only if the output is mp3.

PS ,请确保您的输入mp4使用MP3音频编解码器.如果没有(现在大多数人都使用AAC或AC3),则不足以重新混音文件,还需要对音频流进行解码和重新编码.

PS, make sure that your input mp4 uses the MP3 audio codec. If it does not (and most have AAC or AC3 these days), it's not enough to remux the file, you also need to decode and re-encode the audio stream.

这篇关于复制avcodec参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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