Copying avcodec parameters

假如想象 提交于 2020-06-28 06:35:54

问题


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:

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);

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.

Questions:

  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.

回答1:


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) {

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

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.



来源:https://stackoverflow.com/questions/62195218/copying-avcodec-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!