It behaves similarly to image2 muxer
Luca Barbato authored on 2011/10/03 06:05:29... | ... |
@@ -90,6 +90,7 @@ avconv -i INPUT -c:a pcm_u8 -c:v mpeg2video -f framecrc - |
90 | 90 |
|
91 | 91 |
See also the @ref{crc} muxer. |
92 | 92 |
|
93 |
+@anchor{image2} |
|
93 | 94 |
@section image2 |
94 | 95 |
|
95 | 96 |
Image file muxer. |
... | ... |
@@ -267,4 +268,35 @@ For example a 3D WebM clip can be created using the following command line: |
267 | 267 |
avconv -i sample_left_right_clip.mpg -an -c:v libvpx -metadata STEREO_MODE=left_right -y stereo_clip.webm |
268 | 268 |
@end example |
269 | 269 |
|
270 |
+@section segment |
|
271 |
+ |
|
272 |
+Basic stream segmenter. |
|
273 |
+ |
|
274 |
+The segmenter muxer outputs streams to a number of separate files of nearly |
|
275 |
+fixed duration. Output filename pattern can be set in a fashion similar to |
|
276 |
+@ref{image2}. |
|
277 |
+ |
|
278 |
+Every segment starts with a video keyframe, if a video stream is present. |
|
279 |
+The segment muxer works best with a single constant frame rate video. |
|
280 |
+ |
|
281 |
+Optionally it can generate a flat list of the created segments, one segment |
|
282 |
+per line. |
|
283 |
+ |
|
284 |
+@table @option |
|
285 |
+@item segment_format @var{format} |
|
286 |
+Override the inner container format, by default it is guessed by the filename |
|
287 |
+extension. |
|
288 |
+@item segment_time @var{t} |
|
289 |
+Set segment duration to @var{t} seconds. |
|
290 |
+@item segment_list @var{name} |
|
291 |
+Generate also a listfile named @var{name}. |
|
292 |
+@item segment_list_size @var{size} |
|
293 |
+Overwrite the listfile once it reaches @var{size} entries. |
|
294 |
+@end table |
|
295 |
+ |
|
296 |
+@example |
|
297 |
+avconv -i in.mkv -c copy -map 0 -f segment -list out.list out%03d.nut |
|
298 |
+@end example |
|
299 |
+ |
|
300 |
+ |
|
270 | 301 |
@c man end MUXERS |
... | ... |
@@ -268,6 +268,7 @@ OBJS-$(CONFIG_SAP_DEMUXER) += sapdec.o |
268 | 268 |
OBJS-$(CONFIG_SAP_MUXER) += sapenc.o rtpenc_chain.o |
269 | 269 |
OBJS-$(CONFIG_SDP_DEMUXER) += rtsp.o |
270 | 270 |
OBJS-$(CONFIG_SEGAFILM_DEMUXER) += segafilm.o |
271 |
+OBJS-$(CONFIG_SEGMENT_MUXER) += segment.o |
|
271 | 272 |
OBJS-$(CONFIG_SHORTEN_DEMUXER) += rawdec.o |
272 | 273 |
OBJS-$(CONFIG_SIFF_DEMUXER) += siff.o |
273 | 274 |
OBJS-$(CONFIG_SMACKER_DEMUXER) += smacker.o |
... | ... |
@@ -197,6 +197,7 @@ void av_register_all(void) |
197 | 197 |
av_register_rdt_dynamic_payload_handlers(); |
198 | 198 |
#endif |
199 | 199 |
REGISTER_DEMUXER (SEGAFILM, segafilm); |
200 |
+ REGISTER_MUXER (SEGMENT, segment); |
|
200 | 201 |
REGISTER_DEMUXER (SHORTEN, shorten); |
201 | 202 |
REGISTER_DEMUXER (SIFF, siff); |
202 | 203 |
REGISTER_DEMUXER (SMACKER, smacker); |
203 | 204 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,273 @@ |
0 |
+/* |
|
1 |
+ * Generic segmenter |
|
2 |
+ * Copyright (c) 2011, Luca Barbato |
|
3 |
+ * |
|
4 |
+ * This file is part of Libav. |
|
5 |
+ * |
|
6 |
+ * Libav is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * Libav is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with Libav; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#include <strings.h> |
|
22 |
+#include <float.h> |
|
23 |
+ |
|
24 |
+#include "avformat.h" |
|
25 |
+#include "internal.h" |
|
26 |
+ |
|
27 |
+#include "libavutil/log.h" |
|
28 |
+#include "libavutil/opt.h" |
|
29 |
+#include "libavutil/avstring.h" |
|
30 |
+#include "libavutil/parseutils.h" |
|
31 |
+#include "libavutil/mathematics.h" |
|
32 |
+ |
|
33 |
+typedef struct { |
|
34 |
+ const AVClass *class; /**< Class for private options. */ |
|
35 |
+ int number; |
|
36 |
+ AVFormatContext *avf; |
|
37 |
+ char *format; /**< Set by a private option. */ |
|
38 |
+ char *list; /**< Set by a private option. */ |
|
39 |
+ float time; /**< Set by a private option. */ |
|
40 |
+ int size; /**< Set by a private option. */ |
|
41 |
+ int64_t offset_time; |
|
42 |
+ int64_t recording_time; |
|
43 |
+ int has_video; |
|
44 |
+ AVIOContext *pb; |
|
45 |
+} SegmentContext; |
|
46 |
+ |
|
47 |
+static int segment_start(AVFormatContext *s) |
|
48 |
+{ |
|
49 |
+ SegmentContext *c = s->priv_data; |
|
50 |
+ AVFormatContext *oc = c->avf; |
|
51 |
+ int err = 0; |
|
52 |
+ |
|
53 |
+ if (av_get_frame_filename(oc->filename, sizeof(oc->filename), |
|
54 |
+ s->filename, c->number++) < 0) |
|
55 |
+ return AVERROR(EINVAL); |
|
56 |
+ |
|
57 |
+ if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE, |
|
58 |
+ &s->interrupt_callback, NULL)) < 0) |
|
59 |
+ return err; |
|
60 |
+ |
|
61 |
+ if (!oc->priv_data && oc->oformat->priv_data_size > 0) { |
|
62 |
+ oc->priv_data = av_mallocz(oc->oformat->priv_data_size); |
|
63 |
+ if (!oc->priv_data) { |
|
64 |
+ avio_close(oc->pb); |
|
65 |
+ return AVERROR(ENOMEM); |
|
66 |
+ } |
|
67 |
+ if (oc->oformat->priv_class) { |
|
68 |
+ *(const AVClass**)oc->priv_data = oc->oformat->priv_class; |
|
69 |
+ av_opt_set_defaults(oc->priv_data); |
|
70 |
+ } |
|
71 |
+ } |
|
72 |
+ |
|
73 |
+ if ((err = oc->oformat->write_header(oc)) < 0) { |
|
74 |
+ goto fail; |
|
75 |
+ } |
|
76 |
+ |
|
77 |
+ return 0; |
|
78 |
+ |
|
79 |
+fail: |
|
80 |
+ avio_close(oc->pb); |
|
81 |
+ av_freep(&oc->priv_data); |
|
82 |
+ |
|
83 |
+ return err; |
|
84 |
+} |
|
85 |
+ |
|
86 |
+static int segment_end(AVFormatContext *oc) |
|
87 |
+{ |
|
88 |
+ int ret = 0; |
|
89 |
+ |
|
90 |
+ if (oc->oformat->write_trailer) |
|
91 |
+ ret = oc->oformat->write_trailer(oc); |
|
92 |
+ |
|
93 |
+ avio_close(oc->pb); |
|
94 |
+ if (oc->oformat->priv_class) |
|
95 |
+ av_opt_free(oc->priv_data); |
|
96 |
+ av_freep(&oc->priv_data); |
|
97 |
+ |
|
98 |
+ return ret; |
|
99 |
+} |
|
100 |
+ |
|
101 |
+static int seg_write_header(AVFormatContext *s) |
|
102 |
+{ |
|
103 |
+ SegmentContext *seg = s->priv_data; |
|
104 |
+ AVFormatContext *oc; |
|
105 |
+ int ret, i; |
|
106 |
+ |
|
107 |
+ seg->number = 0; |
|
108 |
+ seg->offset_time = 0; |
|
109 |
+ seg->recording_time = seg->time * 1000000; |
|
110 |
+ |
|
111 |
+ if (seg->list) |
|
112 |
+ if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE, |
|
113 |
+ &s->interrupt_callback, NULL)) < 0) |
|
114 |
+ return ret; |
|
115 |
+ |
|
116 |
+ for (i = 0; i< s->nb_streams; i++) |
|
117 |
+ seg->has_video += |
|
118 |
+ (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO); |
|
119 |
+ |
|
120 |
+ if (seg->has_video > 1) |
|
121 |
+ av_log(s, AV_LOG_WARNING, |
|
122 |
+ "More than a single video stream present, " |
|
123 |
+ "expect issues decoding it.\n"); |
|
124 |
+ |
|
125 |
+ oc = avformat_alloc_context(); |
|
126 |
+ |
|
127 |
+ if (!oc) { |
|
128 |
+ ret = AVERROR(ENOMEM); |
|
129 |
+ goto fail; |
|
130 |
+ } |
|
131 |
+ |
|
132 |
+ oc->oformat = av_guess_format(seg->format, s->filename, NULL); |
|
133 |
+ |
|
134 |
+ if (!oc->oformat) { |
|
135 |
+ ret = AVERROR_MUXER_NOT_FOUND; |
|
136 |
+ goto fail; |
|
137 |
+ } |
|
138 |
+ if (oc->oformat->flags & AVFMT_NOFILE) { |
|
139 |
+ av_log(s, AV_LOG_ERROR, "format %s not supported.\n", |
|
140 |
+ oc->oformat->name); |
|
141 |
+ ret = AVERROR(EINVAL); |
|
142 |
+ goto fail; |
|
143 |
+ } |
|
144 |
+ |
|
145 |
+ seg->avf = oc; |
|
146 |
+ |
|
147 |
+ oc->streams = s->streams; |
|
148 |
+ oc->nb_streams = s->nb_streams; |
|
149 |
+ |
|
150 |
+ if (av_get_frame_filename(oc->filename, sizeof(oc->filename), |
|
151 |
+ s->filename, seg->number++) < 0) { |
|
152 |
+ ret = AVERROR(EINVAL); |
|
153 |
+ goto fail; |
|
154 |
+ } |
|
155 |
+ |
|
156 |
+ if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE, |
|
157 |
+ &s->interrupt_callback, NULL)) < 0) |
|
158 |
+ goto fail; |
|
159 |
+ |
|
160 |
+ if ((ret = avformat_write_header(oc, NULL)) < 0) { |
|
161 |
+ avio_close(oc->pb); |
|
162 |
+ goto fail; |
|
163 |
+ } |
|
164 |
+ |
|
165 |
+ if (seg->list) { |
|
166 |
+ avio_printf(seg->pb, "%s\n", oc->filename); |
|
167 |
+ avio_flush(seg->pb); |
|
168 |
+ } |
|
169 |
+ |
|
170 |
+fail: |
|
171 |
+ if (ret) { |
|
172 |
+ oc->streams = NULL; |
|
173 |
+ oc->nb_streams = 0; |
|
174 |
+ if (seg->list) |
|
175 |
+ avio_close(seg->pb); |
|
176 |
+ avformat_free_context(oc); |
|
177 |
+ } |
|
178 |
+ return ret; |
|
179 |
+} |
|
180 |
+ |
|
181 |
+static int seg_write_packet(AVFormatContext *s, AVPacket *pkt) |
|
182 |
+{ |
|
183 |
+ SegmentContext *seg = s->priv_data; |
|
184 |
+ AVFormatContext *oc = seg->avf; |
|
185 |
+ AVStream *st = oc->streams[pkt->stream_index]; |
|
186 |
+ int64_t end_pts = seg->recording_time * seg->number; |
|
187 |
+ int ret; |
|
188 |
+ |
|
189 |
+ if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) && |
|
190 |
+ av_compare_ts(pkt->pts, st->time_base, |
|
191 |
+ end_pts, AV_TIME_BASE_Q) >= 0 && |
|
192 |
+ pkt->flags & AV_PKT_FLAG_KEY) { |
|
193 |
+ |
|
194 |
+ av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n", |
|
195 |
+ pkt->stream_index, pkt->pts); |
|
196 |
+ |
|
197 |
+ ret = segment_end(oc); |
|
198 |
+ |
|
199 |
+ if (!ret) |
|
200 |
+ ret = segment_start(s); |
|
201 |
+ |
|
202 |
+ if (ret) |
|
203 |
+ goto fail; |
|
204 |
+ |
|
205 |
+ if (seg->list) { |
|
206 |
+ avio_printf(seg->pb, "%s\n", oc->filename); |
|
207 |
+ avio_flush(seg->pb); |
|
208 |
+ if (!(seg->number % seg->size)) { |
|
209 |
+ avio_close(seg->pb); |
|
210 |
+ if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE, |
|
211 |
+ &s->interrupt_callback, NULL)) < 0) |
|
212 |
+ goto fail; |
|
213 |
+ |
|
214 |
+ } |
|
215 |
+ } |
|
216 |
+ } |
|
217 |
+ |
|
218 |
+ ret = oc->oformat->write_packet(oc, pkt); |
|
219 |
+ |
|
220 |
+fail: |
|
221 |
+ if (ret < 0) { |
|
222 |
+ oc->streams = NULL; |
|
223 |
+ oc->nb_streams = 0; |
|
224 |
+ if (seg->list) |
|
225 |
+ avio_close(seg->pb); |
|
226 |
+ avformat_free_context(oc); |
|
227 |
+ } |
|
228 |
+ |
|
229 |
+ return ret; |
|
230 |
+} |
|
231 |
+ |
|
232 |
+static int seg_write_trailer(struct AVFormatContext *s) |
|
233 |
+{ |
|
234 |
+ SegmentContext *seg = s->priv_data; |
|
235 |
+ AVFormatContext *oc = seg->avf; |
|
236 |
+ int ret = segment_end(oc); |
|
237 |
+ if (seg->list) |
|
238 |
+ avio_close(seg->pb); |
|
239 |
+ oc->streams = NULL; |
|
240 |
+ oc->nb_streams = 0; |
|
241 |
+ avformat_free_context(oc); |
|
242 |
+ return ret; |
|
243 |
+} |
|
244 |
+ |
|
245 |
+#define OFFSET(x) offsetof(SegmentContext, x) |
|
246 |
+#define E AV_OPT_FLAG_ENCODING_PARAM |
|
247 |
+static const AVOption options[] = { |
|
248 |
+ { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, |
|
249 |
+ { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E }, |
|
250 |
+ { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E }, |
|
251 |
+ { "segment_list_size", "maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E }, |
|
252 |
+ { NULL }, |
|
253 |
+}; |
|
254 |
+ |
|
255 |
+static const AVClass seg_class = { |
|
256 |
+ .class_name = "segment muxer", |
|
257 |
+ .item_name = av_default_item_name, |
|
258 |
+ .option = options, |
|
259 |
+ .version = LIBAVUTIL_VERSION_INT, |
|
260 |
+}; |
|
261 |
+ |
|
262 |
+ |
|
263 |
+AVOutputFormat ff_segment_muxer = { |
|
264 |
+ .name = "segment", |
|
265 |
+ .long_name = NULL_IF_CONFIG_SMALL("segment muxer"), |
|
266 |
+ .priv_data_size = sizeof(SegmentContext), |
|
267 |
+ .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE, |
|
268 |
+ .write_header = seg_write_header, |
|
269 |
+ .write_packet = seg_write_packet, |
|
270 |
+ .write_trailer = seg_write_trailer, |
|
271 |
+ .priv_class = &seg_class, |
|
272 |
+}; |