-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathremux.c
More file actions
237 lines (191 loc) · 6.43 KB
/
remux.c
File metadata and controls
237 lines (191 loc) · 6.43 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* copyright (c) 2024 Jack Lau
*
* This file is a tutorial about remuxing multimedia file from a container into a new container through ffmpeg API
*
* FFmpeg version 5.0.3
* Tested on MacOS 14.1.2, compiled with clang 14.0.3
*/
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
int main(int argc, char *argv[])
{
int ret = -1;
int idx = -1;
int out_flag = -1;
enum AVMediaType avMt = AVMEDIA_TYPE_UNKNOWN;
int out_streams = 0;
int stream_index = 0;
//deal with arguments
char *src;
char *dst;
int *stream_map = NULL;
AVFormatContext *pFmtCtx = NULL;
AVFormatContext *oFmtCtx = NULL;
const AVOutputFormat *outFmt = NULL;
AVStream *inStream = NULL, *outStream = NULL;
AVPacket *pkt = NULL;
av_log_set_level(AV_LOG_DEBUG);
if(argc < 3){
av_log(NULL, AV_LOG_ERROR, "the arguments must be more than 3!\n");
}
src = argv[1];
dst = argv[2];
pkt = av_packet_alloc();
if (!pkt) {
fprintf(stderr, "Could not allocate AVPacket\n");
return 1;
}
//open the multimedia file
if( (ret = avformat_open_input(&pFmtCtx, src, NULL, NULL)) < 0 ){
av_log(NULL, AV_LOG_ERROR, " %s \n", av_err2str(ret));
exit(-1);
}
avformat_alloc_output_context2(&oFmtCtx, NULL, NULL, dst);
if(!oFmtCtx){
av_log(NULL, AV_LOG_ERROR, "No Memory\n");
goto end;
}
//get the output format
outFmt = av_guess_format(NULL, dst, NULL);
if(!outFmt){
av_log(NULL, AV_LOG_ERROR, "No Memory\n");
goto end;
}
oFmtCtx->oformat = outFmt;
if(oFmtCtx->oformat->video_codec != AV_CODEC_ID_NONE)
{
out_streams++;
avMt = AVMEDIA_TYPE_VIDEO;
}
if(oFmtCtx->oformat->audio_codec != AV_CODEC_ID_NONE)
{
out_streams++;
avMt = AVMEDIA_TYPE_AUDIO;
}
if(oFmtCtx->oformat->subtitle_codec != AV_CODEC_ID_NONE)
{
out_streams++;
avMt = AVMEDIA_TYPE_SUBTITLE;
}
if(out_streams < 2)
{
out_flag = 1;
}
stream_map = av_calloc(pFmtCtx->nb_streams, sizeof(int));
if(!stream_map){
av_log(NULL, AV_LOG_ERROR, "No Memory\n");
goto end;
}
for (int i = 0; i < pFmtCtx->nb_streams; i++)
{
//outStream = NULL;
inStream = pFmtCtx->streams[i];
AVCodecParameters *inCodecPar = inStream->codecpar;
if(inCodecPar->codec_type != AVMEDIA_TYPE_AUDIO &&
inCodecPar->codec_type != AVMEDIA_TYPE_VIDEO &&
inCodecPar->codec_type != AVMEDIA_TYPE_SUBTITLE
)
{
stream_map[i] = -1;
continue;
}
if (inCodecPar->codec_type != avMt && out_flag > 0 )
{
stream_map[i] = -1;
continue;
}
stream_map[i] = stream_index++;
idx = i;
//create a new stream
outStream = avformat_new_stream(oFmtCtx, NULL);
if(!outStream){
av_log(oFmtCtx, AV_LOG_ERROR, "No Memory!\n");
}
//set the arguments of output Stream
avcodec_parameters_copy(outStream->codecpar, inStream->codecpar);
outStream->codecpar->codec_tag = 0;
}
//binding
ret = avio_open2(&oFmtCtx->pb, dst, AVIO_FLAG_WRITE, NULL, NULL);
if(ret < 0){
av_log(oFmtCtx, AV_LOG_ERROR, "%s", av_err2str(ret));
goto end;
}
//write the head file of multimedia to destination file
ret = avformat_write_header(oFmtCtx, NULL);
if(ret < 0){
av_log(oFmtCtx, AV_LOG_ERROR, "%s", av_err2str(ret));
goto end;
}
if(oFmtCtx->oformat->audio_codec == AV_CODEC_ID_NONE && stream_index < 2)
{
while(av_read_frame(pFmtCtx, pkt) >= 0){
if(pkt->stream_index == idx ){
pkt->pts = av_rescale_q_rnd(pkt->pts, inStream->time_base, outStream->time_base, (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
//the dts data maybe different with the the pts if the file is video
pkt->dts = av_rescale_q_rnd(pkt->dts, inStream->time_base, outStream->time_base, (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt->duration = av_rescale_q(pkt->duration, inStream->time_base, outStream->time_base);
pkt->stream_index = 0;
pkt->pos = -1;
av_interleaved_write_frame(oFmtCtx, pkt);
av_packet_unref(pkt);
}
}
}else if(oFmtCtx->oformat->video_codec == AV_CODEC_ID_NONE && stream_index < 2)
{
//read audio data from multimedia files to write into destination file
while(av_read_frame(pFmtCtx, pkt) >= 0){
if(pkt->stream_index == idx ){
pkt->pts = av_rescale_q_rnd(pkt->pts, inStream->time_base, outStream->time_base, (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
//the dts data is same as the the pts if the file is audio
pkt->dts = pkt->pts;
pkt->duration = av_rescale_q(pkt->duration, inStream->time_base, outStream->time_base);
pkt->stream_index = 0;
pkt->pos = -1;
av_interleaved_write_frame(oFmtCtx, pkt);
av_packet_unref(pkt);
}
}
}else
{
//read data from multimedia files to write into destination file
while(av_read_frame(pFmtCtx, pkt) >= 0){
AVStream *inStreamTem, *outStreamTem;
inStreamTem = pFmtCtx->streams[pkt->stream_index];
if(stream_map[stream_index] < 0){
av_packet_unref(pkt);
continue;
}
pkt->stream_index = stream_map[pkt->stream_index];
outStreamTem = oFmtCtx->streams[pkt->stream_index];
av_packet_rescale_ts(pkt, inStreamTem->time_base, outStreamTem->time_base);
pkt->pos = -1;
av_interleaved_write_frame(oFmtCtx, pkt);
av_packet_unref(pkt);
}
}
//write end of file
av_write_trailer(oFmtCtx);
//free memory
end:
if(pFmtCtx){
avformat_close_input(&pFmtCtx);
pFmtCtx = NULL;
}
if(oFmtCtx->pb){
avio_close(oFmtCtx->pb);
}
if(oFmtCtx){
avformat_free_context(oFmtCtx);
oFmtCtx = NULL;
}
if(stream_map){
av_free(stream_map);
}
if (pkt)
{
av_packet_free(&pkt);
}
return 0;
}