#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <stdbool.h>
#define DEBUG_PRINT 1
#if DEBUG_PRINT
#define LOG(fmt, ...) printf(fmt "\n", ##__VA_ARGS__)
#else
#define LOG(fmt, ...)
#endif
int main(int argc, char **argv)
{
AVOutputFormat *ofmt = NULL;
AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
AVPacket pkt;
const char *in_filename, *out_filename;
int ret, i;
int stream_index = 0;
int *stream_mapping = NULL;
int stream_mapping_size = 0;
if (argc < 3)
{
printf("usage: %s input output\n"
"API example program to remux a media file with libavformat and libavcodec.\n"
"The output format is guessed according to the file extension.\n"
"\n", argv[0]);
return 1;
}
in_filename = argv[1];
out_filename = argv[2];
// 打开输入文件
if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0)
{
LOG("Could not open input file '%s'", in_filename);
//goto end;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0)
{
LOG("Failed to retrieve input stream information");
//goto end;
}
av_dump_format(ifmt_ctx, 0, in_filename, 0);
// 确定输出格式
bool push_stream = false;
const char *ofmt_name = NULL;
if (strstr(out_filename, "rtmp://") != NULL)
{
push_stream = true;
ofmt_name = "flv";
}
else if (strstr(out_filename, "udp://") != NULL)
{
push_stream = true;
ofmt_name = "mpegts";
}
else
{
push_stream = false;
ofmt_name = NULL;
}
if ((ret = avformat_alloc_output_context2(&ofmt_ctx, NULL, ofmt_name, out_filename)) < 0)
{
LOG("Could not create output context\n");
goto end;
}
stream_mapping_size = ifmt_ctx->nb_streams;
stream_mapping = (int*)av_mallocz_array(stream_mapping_size, sizeof(*stream_mapping));
if (!stream_mapping) {
ret = AVERROR(ENOMEM);
goto end;
}
ofmt = ofmt_ctx->oformat;
AVRational frame_rate;
double duration;
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
AVStream *out_stream;
AVStream *in_stream = ifmt_ctx->streams[i];
AVCodecParameters *in_codecpar = in_stream->codecpar;
if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO &&
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
{
stream_mapping[i] = -1;
continue;
}
if (push_stream && (in_codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
{
double duration = 0.0;
frame_rate = av_guess_frame_rate(ifmt_ctx, in_stream, NULL);
if (frame_rate.num > 0 && frame_rate.den > 0)
{
AVRational duration_q = { frame_rate.den, frame_rate.num };
duration = av_q2d(duration_q);
}
else
{
// 可选:设置默认帧率,例如 25fps
duration = 0.04;
}
}
stream_mapping[i] = stream_index++;
out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream)
{
LOG("Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
goto end;
}
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar);
if (ret < 0)
{
LOG("Failed to copy codec parameters\n");
goto end;
}
out_stream->codecpar->codec_tag = 0;
}
av_dump_format(ofmt_ctx, 0, out_filename, 1);
if (!(ofmt->flags & AVFMT_NOFILE))
{
if ((ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE)) < 0)
{
LOG("Could not open output file '%s'", out_filename);
goto end;
}
}
if ((ret = avformat_write_header(ofmt_ctx, NULL)) < 0)
{
LOG("Error occurred when opening output file\n");
goto end;
}
while (1) {
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
if (pkt.stream_index >= stream_mapping_size || stream_mapping[pkt.stream_index] < 0) {
av_packet_unref(&pkt);
continue;
}
int codec_type = ifmt_ctx->streams[pkt.stream_index]->codecpar->codec_type;
if (push_stream && (codec_type == AVMEDIA_TYPE_VIDEO))
{
//av_usleep((int64_t)(duration * AV_TIME_BASE));
}
pkt.stream_index = stream_mapping[pkt.stream_index];
AVStream *in_stream = ifmt_ctx->streams[pkt.stream_index];
AVStream *out_stream = ofmt_ctx->streams[pkt.stream_index];
av_packet_rescale_ts(&pkt, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
if (ret < 0) {
LOG("Error muxing packet\n");
break;
}
av_packet_unref(&pkt);
}
av_write_trailer(ofmt_ctx);
end:
avformat_close_input(&ifmt_ctx);
if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE))
avio_closep(&ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
av_freep(&stream_mapping);
if (ret < 0 && ret != AVERROR_EOF)
//printf("Error occurred: %s\n", av_err2str(ret));
return ret < 0 ? 1 : 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.