C语言中使用FFmpeg读取视频文件后将视频流写入其他文件

1.使用ffmpeg复制完整流(根据输入名识别格式)

  1. 代码
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> // 添加 Windows API 支持

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() {
// 设置控制台输出代码页为 UTF-8
SetConsoleOutputCP(65001);
// 设置控制台输入代码页为 UTF-8
SetConsoleCP(65001);
}

int main(int argc, char** argv)
{
// 设置控制台为 UTF-8 编码
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;
}
  1. 编译后运行

    1
    video_stream_copy.exe  input.mp4 video.mp4

2.只复制视频流,不复制音频流

  1. 编码
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
195
196
197
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/timestamp.h>
#include <libavutil/opt.h>
#include <stdio.h>
#include <windows.h> // 添加 Windows API 支持

// 设置控制台编码的函数
void set_console_utf8() {
// 设置控制台输出代码页为 UTF-8
SetConsoleOutputCP(65001);
// 设置控制台输入代码页为 UTF-8
SetConsoleCP(65001);
}

static void log_progress(int64_t frame_count) {
if (frame_count % 100 == 0) {
printf("已处理 %lld 帧\r", frame_count);
fflush(stdout); // 确保输出被刷新
}
}

int main(int argc, char** argv)
{
// 设置控制台为 UTF-8 编码
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;
int64_t frame_count = 0;

if (argc != 3) {
printf("用法: %s <输入文件> <输出文件>\n", argv[0]);
return 1;
}

input_filename = argv[1];
output_filename = argv[2];

printf("开始处理...\n");
printf("输入文件: %s\n", input_filename);
printf("输出文件: %s\n", output_filename);

// 打开输入文件
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;
}

// 初始化为-1(表示不复制)
for (i = 0; i < stream_mapping_size; i++) {
stream_mapping[i] = -1;
}

// 只为视频流创建输出流
for (i = 0; i < input_ctx->nb_streams; i++) {
AVStream* in_stream = input_ctx->streams[i];
AVCodecParameters* in_codecpar = in_stream->codecpar;

// 只处理视频流
if (in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
stream_mapping[i] = -1;
continue;
}

stream_mapping[i] = stream_index++;
AVStream* 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;

printf("找到视频流: %dx%d\n",
in_codecpar->width,
in_codecpar->height);
}

// 打开输出文件
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开始复制视频流...\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;
}

frame_count++;
log_progress(frame_count);

av_packet_unref(pkt);
}

// 写入文件尾
av_write_trailer(output_ctx);
printf("\n\n视频流复制完成!\n");
printf("总共处理了 %lld 帧\n", frame_count);

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;
}
  1. 编译后运行

    1
    video_stream_copy.exe  input.mp4 video.mp4