1 /*
2 * Copyright (c) 2013 Stefano Sabatini
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23 /**
24 * @file libavformat/libavcodec demuxing and muxing API usage example
25 * @example remux.c
26 *
27 * Remux streams from one container format to another. Data is copied from the
28 * input to the output without transcoding.
29 */
30
34
36 {
38
39 printf(
"%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
45 }
46
47 int main(
int argc,
char **argv)
48 {
52 const char *in_filename, *out_filename;
54 int stream_index = 0;
55 int *stream_mapping =
NULL;
56 int stream_mapping_size = 0;
57
58 if (argc < 3) {
59 printf(
"usage: %s input output\n"
60 "API example program to remux a media file with libavformat and libavcodec.\n"
61 "The output format is guessed according to the file extension.\n"
62 "\n", argv[0]);
63 return 1;
64 }
65
66 in_filename = argv[1];
67 out_filename = argv[2];
68
71 fprintf(stderr, "Could not allocate AVPacket\n");
72 return 1;
73 }
74
76 fprintf(stderr, "Could not open input file '%s'", in_filename);
77 goto end;
78 }
79
81 fprintf(stderr, "Failed to retrieve input stream information");
82 goto end;
83 }
84
86
89 fprintf(stderr, "Could not create output context\n");
91 goto end;
92 }
93
95 stream_mapping =
av_calloc(stream_mapping_size,
sizeof(*stream_mapping));
96 if (!stream_mapping) {
98 goto end;
99 }
100
102
107
111 stream_mapping[
i] = -1;
112 continue;
113 }
114
115 stream_mapping[
i] = stream_index++;
116
118 if (!out_stream) {
119 fprintf(stderr, "Failed allocating output stream\n");
121 goto end;
122 }
123
126 fprintf(stderr, "Failed to copy codec parameters\n");
127 goto end;
128 }
130 }
132
136 fprintf(stderr, "Could not open output file '%s'", out_filename);
137 goto end;
138 }
139 }
140
143 fprintf(stderr, "Error occurred when opening output file\n");
144 goto end;
145 }
146
147 while (1) {
149
152 break;
153
158 continue;
159 }
160
164
165 /* copy packet */
169
171 /* pkt is now blank (av_interleaved_write_frame() takes ownership of
172 * its contents and resets pkt), so that no unreferencing is necessary.
173 * This would be different if one used av_write_frame(). */
175 fprintf(stderr, "Error muxing packet\n");
176 break;
177 }
178 }
179
181 end:
183
185
186 /* close output */
190
192
195 return 1;
196 }
197
198 return 0;
199 }