任意格式转为 mp3
java 测试代码
@Test
public void toMp3() {
String inputFile = "G:\\video\\input.flv";
String outputPath = NativeMedia.toMp3(inputFile);
System.out.println(outputPath);
}
c 语言实现
audio_file_utils.h
#ifndef NATIVE_MEDIA_AUDIO_FILE_UTILS_H
#define NATIVE_MEDIA_AUDIO_FILE_UTILS_H
#include <libavformat/avformat.h>
int open_input_file_utf8(AVFormatContext **fmt_ctx, const char *filename);
int open_output_file_utf8(AVIOContext **pb, const char *filename);
#endif
audio_file_utils.c
#include <stdlib.h>
// FFmpeg 头文件
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
#include <libavutil/samplefmt.h>
#ifdef _WIN32
#include <stringapiset.h>
#endif
#include "audio_file_utils.h"
// Helper function to open file with UTF-8 path on Windows
int open_input_file_utf8(AVFormatContext **fmt_ctx, const char *filename) {
#ifdef _WIN32
int wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
if (wlen <= 0) return AVERROR(EINVAL);
wchar_t *wfilename = malloc(wlen * sizeof(wchar_t));
if (!wfilename) return AVERROR(ENOMEM);
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, wlen);
int len = WideCharToMultiByte(CP_UTF8, 0, wfilename, -1, NULL, 0, NULL, NULL);
if (len <= 0) {
free(wfilename);
return AVERROR(EINVAL);
}
char *local_filename = malloc(len);
if (!local_filename) {
free(wfilename);
return AVERROR(ENOMEM);
}
WideCharToMultiByte(CP_UTF8, 0, wfilename, -1, local_filename, len, NULL, NULL);
int ret = avformat_open_input(fmt_ctx, local_filename, NULL, NULL);
free(local_filename);
free(wfilename);
return ret;
#else
return avformat_open_input(fmt_ctx, filename, NULL, NULL);
#endif
}
int open_output_file_utf8(AVIOContext **pb, const char *filename) {
#ifdef _WIN32
int wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
if (wlen <= 0) return AVERROR(EINVAL);
wchar_t *wfilename = malloc(wlen * sizeof(wchar_t));
if (!wfilename) return AVERROR(ENOMEM);
MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, wlen);
int len = WideCharToMultiByte(CP_UTF8, 0, wfilename, -1, NULL, 0, NULL, NULL);
if (len <= 0) {
free(wfilename);
return AVERROR(EINVAL);
}
char *local_filename = malloc(len);
if (!local_filename) {
free(wfilename);
return AVERROR(ENOMEM);
}
WideCharToMultiByte(CP_UTF8, 0, wfilename, -1, local_filename, len, NULL, NULL);
int ret = avio_open(pb, local_filename, AVIO_FLAG_WRITE);
free(local_filename);
free(wfilename);
return ret;
#else
return avio_open(pb, filename, AVIO_FLAG_WRITE);
#endif
}
jni_native_mp3.c
#include "com_litongjava_media_NativeMedia.h"
#include "native_mp3.h"
#include <jni.h>
#include <stdlib.h>
#include <string.h>
#include <libavutil/samplefmt.h>
#ifdef _WIN32
#include <stringapiset.h>
#endif
jstring JNICALL Java_com_litongjava_media_NativeMedia_toMp3(JNIEnv *env, jclass clazz, jstring inputPath) {
// 从 Java 获取输入文件路径(UTF-8 编码)
const char *input_file = (*env)->GetStringUTFChars(env, inputPath, NULL);
if (!input_file) {
return (*env)->NewStringUTF(env, "Error: Failed to get input file path");
}
// 构造输出文件名:如果输入文件名有扩展名则替换为 .mp3,否则追加 .mp3
char *output_file = NULL;
size_t input_len = strlen(input_file);
const char *dot = strrchr(input_file, '.');
if (dot != NULL) {
size_t base_len = dot - input_file;
output_file = (char *) malloc(base_len + 4 + 1); // base + ".mp3" + '\0'
if (!output_file) {
(*env)->ReleaseStringUTFChars(env, inputPath, input_file);
return (*env)->NewStringUTF(env, "Error: Memory allocation failed");
}
strncpy(output_file, input_file, base_len);
output_file[base_len] = '\0';
strcat(output_file, ".mp3");
} else {
output_file = (char *) malloc(input_len + 4 + 1);
if (!output_file) {
(*env)->ReleaseStringUTFChars(env, inputPath, input_file);
return (*env)->NewStringUTF(env, "Error: Memory allocation failed");
}
strcpy(output_file, input_file);
strcat(output_file, ".mp3");
}
jstring result;
char *error_buffer = convert_to_mp3(input_file, output_file);
result = (*env)->NewStringUTF(env, error_buffer);
(*env)->ReleaseStringUTFChars(env, inputPath, input_file);
if (output_file) free(output_file);
return result;
}
native_mp3.c
#include "native_mp3.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// FFmpeg 头文件
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libavutil/audio_fifo.h>
#include <libavutil/mathematics.h>
#ifdef _WIN32
#include <stringapiset.h>
#endif
#include "audio_file_utils.h"
char *convert_to_mp3(const char *input_file, const char *output_file) {
// 初始化各变量
AVFormatContext *input_format_context = NULL;
AVFormatContext *output_format_context = NULL;
SwrContext *swr_context = NULL;
AVCodecContext *decoder_context = NULL;
AVCodecContext *encoder_context = NULL;
const AVCodec *encoder = NULL;
const AVCodec *decoder = NULL;
AVStream *audio_stream = NULL;
AVPacket *input_packet = NULL;
AVPacket *output_packet = NULL;
AVFrame *input_frame = NULL;
AVFrame *output_frame = NULL;
AVAudioFifo *fifo = NULL;
char error_buffer[1024] = {0};
int ret = 0;
int audio_stream_index = -1;
// --- 修改 1: 移除 next_pts,使用更精确的 PTS追踪 ---
// int64_t next_pts = 0; // 移除这个变量
int64_t total_output_samples_written = 0; // 追踪已写入 FIFO 的总输出样本数
int64_t next_encoding_frame_pts = 0; // 追踪下一个编码帧的PTS (简化方法)
// 1. 打开输入文件
ret = open_input_file_utf8(&input_format_context, input_file);
if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not open input file '%s': %s", input_file, error_buffer);
goto cleanup;
}
// 2. 获取流信息
if ((ret = avformat_find_stream_info(input_format_context, NULL)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not find stream info: %s", error_buffer);
goto cleanup;
}
// 3. 查找第一个音频流
for (unsigned int i = 0; i < input_format_context->nb_streams; i++) {
if (input_format_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_index = i;
break;
}
}
if (audio_stream_index == -1) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not find audio stream in input file");
goto cleanup;
}
// 4. 设置解码器
decoder = avcodec_find_decoder(input_format_context->streams[audio_stream_index]->codecpar->codec_id);
if (!decoder) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not find decoder for codec ID %d", input_format_context->streams[audio_stream_index]->codecpar->codec_id);
goto cleanup;
}
decoder_context = avcodec_alloc_context3(decoder);
if (!decoder_context) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate decoder context");
goto cleanup;
}
if ((ret = avcodec_parameters_to_context(decoder_context, input_format_context->streams[audio_stream_index]->codecpar)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not copy decoder parameters: %s", error_buffer);
goto cleanup;
}
// 设置解码器 time_base (很重要!)
decoder_context->time_base = input_format_context->streams[audio_stream_index]->time_base;
if ((ret = avcodec_open2(decoder_context, decoder, NULL)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not open decoder: %s", error_buffer);
goto cleanup;
}
// 5. 设置编码器和输出格式
if ((ret = avformat_alloc_output_context2(&output_format_context, NULL, "mp3", output_file)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate output context: %s", error_buffer);
goto cleanup;
}
encoder = avcodec_find_encoder_by_name("libmp3lame");
if (!encoder) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not find libmp3lame encoder");
goto cleanup;
}
audio_stream = avformat_new_stream(output_format_context, NULL);
if (!audio_stream) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not create new audio stream");
goto cleanup;
}
encoder_context = avcodec_alloc_context3(encoder);
if (!encoder_context) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate encoder context");
goto cleanup;
}
// 设置编码器参数
encoder_context->sample_rate = decoder_context->sample_rate;
encoder_context->bit_rate = 128000;
encoder_context->sample_fmt = AV_SAMPLE_FMT_S16P; // libmp3lame 通常使用 s16p
#if LIBAVUTIL_VERSION_MAJOR < 57
encoder_context->channels = decoder_context->channels;
encoder_context->channel_layout = decoder_context->channel_layout;
#else
av_channel_layout_copy(&encoder_context->ch_layout, &decoder_context->ch_layout);
#endif
if (output_format_context->oformat->flags & AVFMT_GLOBALHEADER) {
encoder_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
if ((ret = avcodec_open2(encoder_context, encoder, NULL)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not open encoder: %s", error_buffer);
goto cleanup;
}
if ((ret = avcodec_parameters_from_context(audio_stream->codecpar, encoder_context)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not copy encoder parameters: %s", error_buffer);
goto cleanup;
}
// 设置输出流 time_base
audio_stream->time_base = (AVRational){1, encoder_context->sample_rate};
// 6. 设置重采样器
swr_context = swr_alloc();
if (!swr_context) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate resampler context");
goto cleanup;
}
#if LIBAVUTIL_VERSION_MAJOR < 57
av_opt_set_int(swr_context, "in_channel_layout", decoder_context->channel_layout, 0);
av_opt_set_int(swr_context, "out_channel_layout", encoder_context->channel_layout, 0);
av_opt_set_int(swr_context, "in_channel_count", decoder_context->channels, 0);
av_opt_set_int(swr_context, "out_channel_count", encoder_context->channels, 0);
#else
av_opt_set_chlayout(swr_context, "in_chlayout", &decoder_context->ch_layout, 0);
av_opt_set_chlayout(swr_context, "out_chlayout", &encoder_context->ch_layout, 0);
#endif
av_opt_set_int(swr_context, "in_sample_rate", decoder_context->sample_rate, 0);
av_opt_set_int(swr_context, "out_sample_rate", encoder_context->sample_rate, 0);
av_opt_set_sample_fmt(swr_context, "in_sample_fmt", decoder_context->sample_fmt, 0);
av_opt_set_sample_fmt(swr_context, "out_sample_fmt", encoder_context->sample_fmt, 0);
if ((ret = swr_init(swr_context)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not initialize resampler: %s", error_buffer);
goto cleanup;
}
// 7. 打开输出文件
if (!(output_format_context->oformat->flags & AVFMT_NOFILE)) {
ret = open_output_file_utf8(&output_format_context->pb, output_file);
if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not open output file '%s': %s", output_file, error_buffer);
goto cleanup;
}
}
// 8. 写输出文件头
if ((ret = avformat_write_header(output_format_context, NULL)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not write output header: %s", error_buffer);
goto cleanup;
}
// 9. 初始化 FIFO
#if LIBAVUTIL_VERSION_MAJOR < 57
fifo = av_audio_fifo_alloc(encoder_context->sample_fmt, encoder_context->channels, 1);
#else
fifo = av_audio_fifo_alloc(encoder_context->sample_fmt, encoder_context->ch_layout.nb_channels, 1);
#endif
if (!fifo) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate FIFO");
goto cleanup;
}
// 10. 分配数据包与帧
input_packet = av_packet_alloc();
output_packet = av_packet_alloc();
input_frame = av_frame_alloc();
output_frame = av_frame_alloc();
if (!input_packet || !output_packet || !input_frame || !output_frame) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate packet or frame");
goto cleanup;
}
// 为重采样后的数据准备 output_frame(临时缓冲区)
output_frame->format = encoder_context->sample_fmt;
#if LIBAVUTIL_VERSION_MAJOR < 57
output_frame->channel_layout = encoder_context->channel_layout;
output_frame->channels = encoder_context->channels;
#else
av_channel_layout_copy(&output_frame->ch_layout, &encoder_context->ch_layout);
#endif
// 设置一个默认采样数(实际将由 swr_convert 返回)
output_frame->nb_samples = encoder_context->frame_size > 0 ? encoder_context->frame_size : 1152;
if ((ret = av_frame_get_buffer(output_frame, 0)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate output frame buffer: %s", error_buffer);
goto cleanup;
}
// --- 修改 2: 初始化新的 PTS 变量 ---
total_output_samples_written = 0;
next_encoding_frame_pts = 0; // 初始化
// --- 解码和编码主循环 ---
// 11. 读取输入包
while (1) {
AVPacket *pkt_to_send = NULL;
if (!(ret = av_read_frame(input_format_context, input_packet)) && input_packet->stream_index == audio_stream_index) {
pkt_to_send = input_packet;
} else if (ret == AVERROR_EOF) {
// 文件读取完毕,发送 NULL 包冲刷解码器
pkt_to_send = NULL;
} else if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error reading frame: %s", error_buffer);
goto cleanup;
} else {
// 不是音频包,跳过
av_packet_unref(input_packet);
continue;
}
ret = avcodec_send_packet(decoder_context, pkt_to_send);
if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error sending packet to decoder: %s", error_buffer);
if(pkt_to_send) av_packet_unref(input_packet);
goto cleanup;
}
if(pkt_to_send) av_packet_unref(input_packet); // 发送成功后立即释放
// 12. 从解码器接收帧
while (1) {
ret = avcodec_receive_frame(decoder_context, input_frame);
if (ret == AVERROR(EAGAIN)) {
// 需要更多数据包才能解码
break;
} else if (ret == AVERROR_EOF) {
// 解码器已冲刷完毕
goto flush_encoder; // 跳出读包循环,进入编码器冲刷阶段
} else if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error receiving frame from decoder: %s", error_buffer);
goto cleanup;
}
// --- 修改 3: 获取并转换输入帧的 PTS ---
int64_t input_pts = input_frame->pts;
if (input_pts == AV_NOPTS_VALUE) {
// 如果没有PTS,回退到基于样本计数的估算 (这可能不够准确,但比完全不用好)
// 使用当前已写入的样本数作为PTS
input_pts = av_rescale_q(total_output_samples_written,
(AVRational){1, encoder_context->sample_rate},
input_format_context->streams[audio_stream_index]->time_base);
}
// 将输入PTS转换到编码器的时间基 (通常是 1/sample_rate)
int64_t output_pts = av_rescale_q_rnd(input_pts,
input_format_context->streams[audio_stream_index]->time_base,
(AVRational){1, encoder_context->sample_rate},
AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
if (output_pts == AV_NOPTS_VALUE) {
// 如果转换后还是无效,回退到基于样本计数
output_pts = total_output_samples_written;
}
// --- 修改 3 结束 ---
// 13. 确保 output_frame 可写
if ((ret = av_frame_make_writable(output_frame)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error making frame writable: %s", error_buffer);
av_frame_unref(input_frame);
goto cleanup;
}
// 14. 重采样转换
int nb_samples_converted = swr_convert(swr_context,
output_frame->data, output_frame->nb_samples,
(const uint8_t **) input_frame->data, input_frame->nb_samples);
if (nb_samples_converted < 0) {
av_strerror(nb_samples_converted, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error converting audio: %s", error_buffer);
av_frame_unref(input_frame);
goto cleanup;
}
output_frame->nb_samples = nb_samples_converted; // 更新实际转换的样本数
// --- 修改 4: 更新总输出样本数 (在写入FIFO之前) ---
total_output_samples_written += nb_samples_converted;
// --- 修改 4 结束 ---
// 15. 将转换后的样本写入 FIFO
if (av_audio_fifo_write(fifo, (void **) output_frame->data, nb_samples_converted) < nb_samples_converted) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not write data to FIFO");
av_frame_unref(input_frame);
goto cleanup;
}
// --- 移除旧的 next_pts 累加逻辑 ---
// int64_t frame_duration = av_rescale_q(input_frame->nb_samples, ...);
// next_pts += frame_duration;
// --- 移除结束 ---
av_frame_unref(input_frame); // 处理完立即释放
// 17. 当 FIFO 中样本足够构成一帧时,从 FIFO 中读取固定数量样本送编码器
while (av_audio_fifo_size(fifo) >= encoder_context->frame_size) {
AVFrame *enc_frame = av_frame_alloc();
if (!enc_frame) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate encoding frame");
goto cleanup;
}
enc_frame->nb_samples = encoder_context->frame_size;
enc_frame->format = encoder_context->sample_fmt;
#if LIBAVUTIL_VERSION_MAJOR < 57
enc_frame->channel_layout = encoder_context->channel_layout;
enc_frame->channels = encoder_context->channels;
#else
av_channel_layout_copy(&enc_frame->ch_layout, &encoder_context->ch_layout);
#endif
if ((ret = av_frame_get_buffer(enc_frame, 0)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate buffer for encoding frame: %s", error_buffer);
av_frame_free(&enc_frame);
goto cleanup;
}
if (av_audio_fifo_read(fifo, (void **) enc_frame->data, encoder_context->frame_size) < encoder_context->frame_size) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not read data from FIFO");
av_frame_free(&enc_frame);
goto cleanup;
}
// --- 修改 5: 为编码帧分配正确的 PTS (使用简化追踪方法) ---
enc_frame->pts = next_encoding_frame_pts;
next_encoding_frame_pts += encoder_context->frame_size; // 更新下一个帧的PTS
// --- 修改 5 结束 ---
ret = avcodec_send_frame(encoder_context, enc_frame);
if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error sending frame to encoder: %s", error_buffer);
av_frame_free(&enc_frame);
goto cleanup;
}
av_frame_free(&enc_frame);
// 18. 从编码器接收数据包并写入输出文件
while (1) {
ret = avcodec_receive_packet(encoder_context, output_packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error receiving packet from encoder: %s", error_buffer);
goto cleanup;
}
output_packet->stream_index = 0;
// 编码器的 time_base 通常与流的 time_base 相同或相关
av_packet_rescale_ts(output_packet,
encoder_context->time_base,
output_format_context->streams[0]->time_base);
ret = av_interleaved_write_frame(output_format_context, output_packet);
if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error writing packet: %s", error_buffer);
av_packet_unref(output_packet);
goto cleanup;
}
av_packet_unref(output_packet);
}
}
} // End of receive frame loop
} // End of read packet loop
flush_encoder:
// --- 修改 6: 正确处理 FIFO 中剩余样本 ---
// 处理 FIFO 中剩余不足一帧的样本
while (av_audio_fifo_size(fifo) > 0) {
int remaining_samples = av_audio_fifo_size(fifo);
// 读取所有剩余样本,即使少于一帧
int samples_to_read = remaining_samples;
AVFrame *enc_frame = av_frame_alloc();
if (!enc_frame) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate final encoding frame");
goto cleanup;
}
enc_frame->nb_samples = samples_to_read;
enc_frame->format = encoder_context->sample_fmt;
#if LIBAVUTIL_VERSION_MAJOR < 57
enc_frame->channel_layout = encoder_context->channel_layout;
enc_frame->channels = encoder_context->channels;
#else
av_channel_layout_copy(&enc_frame->ch_layout, &encoder_context->ch_layout);
#endif
// Use av_frame_get_buffer with align=0 for potentially non-standard nb_samples
if ((ret = av_frame_get_buffer(enc_frame, 0)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not allocate buffer for final frame: %s", error_buffer);
av_frame_free(&enc_frame);
goto cleanup;
}
if (av_audio_fifo_read(fifo, (void **) enc_frame->data, samples_to_read) < samples_to_read) {
snprintf(error_buffer, sizeof(error_buffer), "Error: Could not read final data from FIFO");
av_frame_free(&enc_frame);
goto cleanup;
}
// --- 修改 7: 为最后的帧分配正确的 PTS ---
enc_frame->pts = next_encoding_frame_pts; // 使用追踪到的PTS
next_encoding_frame_pts += samples_to_read; // 更新 (虽然循环会结束,但保持逻辑一致)
// --- 修改 7 结束 ---
ret = avcodec_send_frame(encoder_context, enc_frame);
if (ret < 0 && ret != AVERROR_EOF) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error sending final frame to encoder: %s", error_buffer);
av_frame_free(&enc_frame);
goto cleanup;
}
av_frame_free(&enc_frame);
while (1) {
ret = avcodec_receive_packet(encoder_context, output_packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error receiving final packet from encoder: %s", error_buffer);
goto cleanup;
}
output_packet->stream_index = 0;
av_packet_rescale_ts(output_packet,
encoder_context->time_base,
output_format_context->streams[0]->time_base);
ret = av_interleaved_write_frame(output_format_context, output_packet);
if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error writing final packet: %s", error_buffer);
av_packet_unref(output_packet);
goto cleanup;
}
av_packet_unref(output_packet);
}
}
// --- 修改 6 结束 ---
// 20. 冲刷编码器(发送 NULL 帧)
ret = avcodec_send_frame(encoder_context, NULL);
if (ret < 0 && ret != AVERROR_EOF) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error flushing encoder (send NULL): %s", error_buffer);
goto cleanup;
}
while (1) {
ret = avcodec_receive_packet(encoder_context, output_packet);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
break;
else if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error flushing encoder (receive): %s", error_buffer);
goto cleanup;
}
output_packet->stream_index = 0;
av_packet_rescale_ts(output_packet,
encoder_context->time_base,
output_format_context->streams[0]->time_base);
ret = av_interleaved_write_frame(output_format_context, output_packet);
if (ret < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error writing flushed packet: %s", error_buffer);
av_packet_unref(output_packet);
goto cleanup;
}
av_packet_unref(output_packet);
}
// 21. 写文件尾
if ((ret = av_write_trailer(output_format_context)) < 0) {
av_strerror(ret, error_buffer, sizeof(error_buffer));
snprintf(error_buffer, sizeof(error_buffer), "Error writing trailer: %s", error_buffer);
goto cleanup;
}
// 成功时将输出文件路径作为结果返回
strncpy(error_buffer, output_file, sizeof(error_buffer) - 1);
error_buffer[sizeof(error_buffer) - 1] = '\0';
cleanup:
if (input_frame) av_frame_free(&input_frame);
if (output_frame) av_frame_free(&output_frame);
if (input_packet) av_packet_free(&input_packet);
if (output_packet) av_packet_free(&output_packet);
if (decoder_context) avcodec_free_context(&decoder_context);
if (encoder_context) avcodec_free_context(&encoder_context);
if (swr_context) swr_free(&swr_context);
if (fifo) av_audio_fifo_free(fifo);
if (input_format_context) avformat_close_input(&input_format_context);
if (output_format_context) {
if (!(output_format_context->oformat->flags & AVFMT_NOFILE) && output_format_context->pb) {
avio_closep(&output_format_context->pb);
}
avformat_free_context(output_format_context);
}
// Return a copy of the error message or output path
char* result = malloc(strlen(error_buffer) + 1);
if (result) {
strcpy(result, error_buffer);
}
return result;
}