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
| #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/timestamp.h> #include <libavutil/opt.h> #include <stdio.h> #include <windows.h>
static void log_packet(const AVFormatContext* fmt_ctx, const AVPacket* pkt, const char* tag) { AVRational* time_base = &fmt_ctx->streams[pkt->stream_index]->time_base; printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n", tag, av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base), av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base), av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base), pkt->stream_index); }
void set_console_utf8() { SetConsoleOutputCP(65001); SetConsoleCP(65001); }
int main(int argc, char** argv) { set_console_utf8();
const char* input_filename, * output_filename; AVFormatContext* input_ctx = NULL, * output_ctx = NULL; AVPacket* pkt = NULL; int ret, i; int stream_index = 0; int* stream_mapping = NULL; int stream_mapping_size = 0;
if (argc != 3) { printf("用法: %s <输入文件> <输出文件>\n", argv[0]); return 1; }
input_filename = argv[1]; output_filename = argv[2];
if ((ret = avformat_open_input(&input_ctx, input_filename, NULL, NULL)) < 0) { fprintf(stderr, "无法打开输入文件 '%s'\n", input_filename); goto end; }
if ((ret = avformat_find_stream_info(input_ctx, NULL)) < 0) { fprintf(stderr, "无法获取流信息\n"); goto end; }
avformat_alloc_output_context2(&output_ctx, NULL, NULL, output_filename); if (!output_ctx) { fprintf(stderr, "无法创建输出上下文\n"); ret = AVERROR_UNKNOWN; goto end; }
stream_mapping_size = input_ctx->nb_streams; stream_mapping = av_calloc(stream_mapping_size, sizeof(*stream_mapping)); if (!stream_mapping) { ret = AVERROR(ENOMEM); goto end; }
for (i = 0; i < input_ctx->nb_streams; i++) { AVStream* out_stream; AVStream* in_stream = input_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; }
stream_mapping[i] = stream_index++;
out_stream = avformat_new_stream(output_ctx, NULL); if (!out_stream) { fprintf(stderr, "无法分配输出流\n"); ret = AVERROR_UNKNOWN; goto end; }
ret = avcodec_parameters_copy(out_stream->codecpar, in_codecpar); if (ret < 0) { fprintf(stderr, "无法复制编解码器参数\n"); goto end; } out_stream->codecpar->codec_tag = 0; }
if (!(output_ctx->oformat->flags & AVFMT_NOFILE)) { ret = avio_open(&output_ctx->pb, output_filename, AVIO_FLAG_WRITE); if (ret < 0) { fprintf(stderr, "无法打开输出文件 '%s'\n", output_filename); goto end; } }
ret = avformat_write_header(output_ctx, NULL); if (ret < 0) { fprintf(stderr, "写入文件头错误\n"); goto end; }
pkt = av_packet_alloc(); if (!pkt) { fprintf(stderr, "无法分配 AVPacket\n"); ret = AVERROR(ENOMEM); goto end; }
printf("开始复制视频流...\n"); while (1) { AVStream* in_stream, * out_stream;
ret = av_read_frame(input_ctx, pkt); if (ret < 0) break;
in_stream = input_ctx->streams[pkt->stream_index]; if (pkt->stream_index >= stream_mapping_size || stream_mapping[pkt->stream_index] < 0) { av_packet_unref(pkt); continue; }
pkt->stream_index = stream_mapping[pkt->stream_index]; out_stream = output_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(output_ctx, pkt); if (ret < 0) { fprintf(stderr, "写入数据包时错误\n"); break; } av_packet_unref(pkt); }
av_write_trailer(output_ctx); printf("视频流复制完成!\n");
end: av_packet_free(&pkt); av_freep(&stream_mapping);
if (output_ctx && !(output_ctx->oformat->flags & AVFMT_NOFILE)) avio_closep(&output_ctx->pb); avformat_free_context(output_ctx); avformat_close_input(&input_ctx);
if (ret < 0) { fprintf(stderr, "发生错误\n"); return 1; }
return 0; }
|