ffmpeg 混音代码
时间: 2023-10-08 22:08:12 浏览: 26
以下是使用FFmpeg混音的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
int main(int argc, char *argv[]) {
AVFormatContext *formatCtx1 = NULL;
AVFormatContext *formatCtx2 = NULL;
AVFormatContext *outputFormatCtx = NULL;
AVStream *audioStream1 = NULL;
AVStream *audioStream2 = NULL;
AVCodecContext *audioCodecCtx1 = NULL;
AVCodecContext *audioCodecCtx2 = NULL;
AVCodec *audioCodec = NULL;
AVFrame *frame1 = NULL;
AVFrame *frame2 = NULL;
AVPacket packet;
const char *inputFile1 = "input1.mp3";
const char *inputFile2 = "input2.mp3";
const char *outputFile = "output.mp3";
int ret;
av_register_all();
// 打开第一个输入文件
ret = avformat_open_input(&formatCtx1, inputFile1, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open input file '%s'\n", inputFile1);
return 1;
}
// 获取音频流信息
ret = avformat_find_stream_info(formatCtx1, NULL);
if (ret < 0) {
fprintf(stderr, "Could not find stream information\n");
return 1;
}
// 打开第二个输入文件
ret = avformat_open_input(&formatCtx2, inputFile2, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open input file '%s'\n", inputFile2);
return 1;
}
// 获取音频流信息
ret = avformat_find_stream_info(formatCtx2, NULL);
if (ret < 0) {
fprintf(stderr, "Could not find stream information\n");
return 1;
}
// 创建输出文件上下文
ret = avformat_alloc_output_context2(&outputFormatCtx, NULL, NULL, outputFile);
if (ret < 0) {
fprintf(stderr, "Could not create output context\n");
return 1;
}
// 查找音频流
for (int i = 0; i < formatCtx1->nb_streams; i++) {
if (formatCtx1->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream1 = formatCtx1->streams[i];
audioCodecCtx1 = avcodec_alloc_context3(NULL);
ret = avcodec_parameters_to_context(audioCodecCtx1, audioStream1->codecpar);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parameters to decoder context\n");
return 1;
}
// 打开解码器
audioCodec = avcodec_find_decoder(audioCodecCtx1->codec_id);
if (!audioCodec) {
fprintf(stderr, "Unsupported codec\n");
return 1;
}
ret = avcodec_open2(audioCodecCtx1, audioCodec, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open codec\n");
return 1;
}
break;
}
}
// 查找音频流
for (int i = 0; i < formatCtx2->nb_streams; i++) {
if (formatCtx2->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream2 = formatCtx2->streams[i];
audioCodecCtx2 = avcodec_alloc_context3(NULL);
ret = avcodec_parameters_to_context(audioCodecCtx2, audioStream2->codecpar);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parameters to decoder context\n");
return 1;
}
// 打开解码器
audioCodec = avcodec_find_decoder(audioCodecCtx2->codec_id);
if (!audioCodec) {
fprintf(stderr, "Unsupported codec\n");
return 1;
}
ret = avcodec_open2(audioCodecCtx2, audioCodec, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open codec\n");
return 1;
}
break;
}
}
// 创建输出流
AVStream *outputStream = avformat_new_stream(outputFormatCtx, NULL);
if (!outputStream) {
fprintf(stderr, "Failed to create output stream\n");
return 1;
}
// 复制音频流参数
ret = avcodec_parameters_copy(outputStream->codecpar, audioStream1->codecpar);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parameters\n");
return 1;
}
// 打开输出文件
ret = avio_open(&outputFormatCtx->pb, outputFile, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open output file '%s'\n", outputFile);
return 1;
}
// 写文件头
ret = avformat_write_header(outputFormatCtx, NULL);
if (ret < 0) {
fprintf(stderr, "Error occurred when writing header\n");
return 1;
}
// 初始化帧和数据包
frame1 = av_frame_alloc();
frame2 = av_frame_alloc();
av_init_packet(&packet);
packet.data = NULL;
packet.size = 0;
// 读取第一个输入文件的音频帧
while (av_read_frame(formatCtx1, &packet) >= 0) {
if (packet.stream_index == audioStream1->index) {
ret = avcodec_send_packet(audioCodecCtx1, &packet);
if (ret < 0) {
fprintf(stderr, "Error sending a packet to the decoder\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(audioCodecCtx1, frame1);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
fprintf(stderr, "Error during decoding\n");
return 1;
}
// 读取第二个输入文件的音频帧
if (av_read_frame(formatCtx2, &packet) >= 0 && packet.stream_index == audioStream2->index) {
ret = avcodec_send_packet(audioCodecCtx2, &packet);
if (ret < 0) {
fprintf(stderr, "Error sending a packet to the decoder\n");
break;
}
while (ret >= 0) {
ret = avcodec_receive_frame(audioCodecCtx2, frame2);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
fprintf(stderr, "Error during decoding\n");
return 1;
}
// 混音
for (int i = 0; i < frame1->nb_samples; i++) {
for (int ch = 0; ch < audioCodecCtx1->channels; ch++) {
((int16_t *)frame1->data[ch])[i] += ((int16_t *)frame2->data[ch])[i];
}
}
// 编码并写入输出文件
ret = avcodec_send_frame(audioCodecCtx1, frame1);
if (ret < 0) {
fprintf(stderr, "Error sending a frame for encoding\n");
return 1;
}
while (ret >= 0) {
ret = avcodec_receive_packet(audioCodecCtx1, &packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
return 1;
}
packet.stream_index = outputStream->index;
av_packet_rescale_ts(&packet, audioCodecCtx1->time_base, outputStream->time_base);
ret = av_write_frame(outputFormatCtx, &packet);
av_packet_unref(&packet);
if (ret < 0) {
fprintf(stderr, "Error writing frame\n");
return 1;
}
}
}
av_packet_unref(&packet);
}
}
}
av_packet_unref(&packet);
}
// 写文件尾
ret = av_write_trailer(outputFormatCtx);
if (ret < 0) {
fprintf(stderr, "Error occurred when writing trailer\n");
return 1;
}
// 清理资源
avcodec_free_context(&audioCodecCtx1);
avcodec_free_context(&audioCodecCtx2);
avformat_close_input(&formatCtx1);
avformat_close_input(&formatCtx2);
av_frame_free(&frame1);
av_frame_free(&frame2);
avformat_free_context(outputFormatCtx);
return 0;
}
```
请确保在编译时链接FFmpeg的库。另外,请根据实际情况修改输入文件和输出文件的路径。这个示例代码将两个输入文件的音频流混合到一个输出文件中。