如何使用FFmpeg将立体声转换为单声道?

Meu*_*ara 6 c++ audio ffmpeg function

我将FFmpeg库用于个人项目,并且我需要一件事的帮助。我有立体声音乐文件,我想将此立体声转换为单声道声音吗?这个图书馆有可能吗?内部有功能可以完成这项工作吗?我的项目是C / C ++。

我在FFmpeg网站和此论坛上搜索了Doxygen文档,但没有发现任何有趣的东西。

谢谢阅读 !

Nee*_*kla 11

您可以只使用ffmpeg。出于以下目的而存在直接命令:

ffmpeg -i stereo.flac -ac 1 mono.flac
Run Code Online (Sandbox Code Playgroud)

将您的立体声文件转换成单声道。有关更多详细信息,您可以查看此页面-

https://trac.ffmpeg.org/wiki/AudioChannelManipulation

  • 请注意,“-ac 1”会将两个立体声通道**混合**为一个单声道,这可能不是您想要的,特别是如果它只是“错误地以立体声录制的单声道源”。在这种情况下,请像这样丢弃其中一个通道:`ffmpeg -i INPUT -filter_complex '[0:a]channelsplit=channel_layout=stereo:channels=FL[left]' -map '[left]' OUTPUT`(替换当然是“输入”和“输出”)。这将选择左通道,如果您想要右通道,请使用“FR”和“[right]”代替。 (6认同)
  • @MarcoArruda您可以通过直接调用“ffmpeg”来跳过“convert.sh”文件:“find”。-name '*.mp4' -exec ffmpeg -i '{}' -ac 1 '{}.mono.mp4' \;` (5认同)
  • 谢谢,给了我很大帮助!顺便说一句,如果其他人需要像我一样连续转换大量文件:`find . -name '*.mp4' -exec ./convert.sh {} \;` (2认同)

Ste*_*e M 5

使用swr_convert来自libswresample的格式之间进行转换。就像是:

#include "libswresample/swresample.h"

au_convert_ctx = swr_alloc();

out_channel_layout = AV_CH_LAYOUT_MONO;
out_sample_fmt = AV_SAMPLE_FMT_S16;
out_sample_rate = 44100;
out_channels = av_get_channel_layout_nb_channels(out_channel_layout);

in_sample_fmt = pCodecCtx->sample_fmt;
in_channel_layout=av_get_default_channel_layout(pCodecCtx->channels);

au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,out_channel_layout, out_sample_fmt, out_sample_rate,
            in_channel_layout, in_sample_fmt, pCodecCtx->sample_rate, 0, NULL);
swr_init(au_convert_ctx);
//Generate your frame of original audio, then use swr_convert to convert to mono,
//converted number of samples will now be in out_buffer.
int converted = swr_convert(au_convert_ctx, &out_buffer, MAX_AUDIO_FRAME_SIZE, (const uint8_t **)&pFrame->data , pFrame->nb_samples);
//...
swr_free(&au_convert_ctx);
Run Code Online (Sandbox Code Playgroud)

让您开始。这会将原始格式碰巧转换为44100 kHz单声道。您也可以将其pCodecCtx->sample_rate用作输出采样率。

这是最灵活,最简单的解决方案。