FFmpeg
Loading...
Searching...
No Matches
hlsenc.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "libavformat/hlsenc.c"
20
21/* Exercise URL generation without opening network connections or files. */
22static int io_open(AVFormatContext *s, AVIOContext **pb, const char *url,
24{
25 return avio_open_dyn_buf(pb);
26}
27
29{
31 return 0;
32}
33
34static int test_filename(const char *url, int single_file, int fmp4)
35{
37 AVStream *st;
39 HLSContext *hls;
40 VariantStream *vs;
41 int ret;
42
44 if (ret < 0)
45 return ret;
46
47 s->io_open = io_open;
48 s->io_close2 = io_close;
49 hls = s->priv_data;
50
52 if (!st) {
53 ret = AVERROR(ENOMEM);
54 goto end;
55 }
58 st->codecpar->width = 16;
59 st->codecpar->height = 16;
60 st->time_base = (AVRational) { 1, 25 };
61
63 if (!st) {
64 ret = AVERROR(ENOMEM);
65 goto end;
66 }
69 st->time_base = (AVRational) { 1, 1000 };
70
71 /* Avoid filesystem renames when writing the in-memory playlists. */
72 ret = av_dict_set(&options, "hls_playlist_type", "vod", 0);
73 if (ret < 0)
74 goto end;
75
76 if (single_file) {
77 ret = av_dict_set(&options, "hls_flags", "single_file", 0);
78 if (ret < 0)
79 goto end;
80 }
81 if (fmp4) {
82 ret = av_dict_set(&options, "hls_segment_type", "fmp4", 0);
83 if (ret < 0)
84 goto end;
85 }
86
88 if (ret < 0)
89 goto end;
90
91 vs = &hls->var_streams[0];
92 printf("%s (%s, %s)\n", url, fmp4 ? "fmp4" : "mpegts",
93 single_file ? "single file" : "segments");
94 printf("segment: %s\n", vs->avf->url);
95 printf("subtitle: %s\n", vs->vtt_avf->url);
96 printf("subtitle playlist: %s\n", vs->vtt_m3u8_name);
97 printf("playlist: %s\n", vs->m3u8_name);
99
100end:
103 return ret;
104}
105
106int main(int argc, char **argv)
107{
108 static const char *const urls[] = {
109 "https://example.ffmpeg",
110 "https://example.ffmpeg/",
111 "https://user.name:password@example.ffmpeg:8443/dir.name/output",
112 "https://example.ffmpeg/dir.name/output.m3u8",
113 "https://example.ffmpeg/dir.name/output.m3u8?token=foo.bar",
114 "https://example.ffmpeg/dir.name/output.m3u8#foo.bar",
115 "https://example.ffmpeg/dir.name/output.m3u8?token=foo.bar#baz.qux",
116 "dir.name/output",
117 "dir.name/output.m3u8",
118 "foo#bar.ts",
119 "foo?bar.ts",
120 "foo#bar",
121 "foo?bar",
122 "file:foo#bar.ts",
123 "file:foo?bar.ts",
124 "foo.m3u8?token=foo.bar",
125 "file:foo.m3u8?token=foo.bar",
126 "dir#tag/output.m3u8",
127 "dir?tag/output.m3u8",
128 "file:dir#tag/output.m3u8",
129 "file:dir?tag/output.m3u8",
130 };
131 static const char *const file_urls[] = {
132 "capture_1:take#2/output.m3u8",
133 "capture_1:take?2/output.m3u8",
134 "my video:dir#tag/output.m3u8",
135 "my video:dir?tag/output.m3u8",
136 "file://[dir/output.m3u8",
137 "crypto:file:dir#tag/output.m3u8",
138 "crypto+file:dir?tag/output.m3u8",
139 "crypto:dir#tag/output.m3u8",
140 "crypto:dir?tag/output.m3u8",
141 "crypto:crypto+file:dir#tag/output.m3u8",
142 "crypto+crypto:file:dir?tag/output.m3u8",
143 "crypto:capture_1:take#2/output.m3u8",
144 "crypto+file://[dir/output.m3u8",
145 };
146 const char *const *test_urls = urls;
147 int nb_urls = FF_ARRAY_ELEMS(urls);
148 int ret;
149
150 if (argc == 2 && !strcmp(argv[1], "file")) {
151 test_urls = file_urls;
152 nb_urls = FF_ARRAY_ELEMS(file_urls);
153 } else if (argc != 1) {
154 fprintf(stderr, "Usage: %s [file]\n", argv[0]);
155 return 1;
156 }
157
159
160 for (int i = 0; i < nb_urls; i++) {
161 for (int single_file = 0; single_file < 2; single_file++) {
162 for (int fmp4 = 0; fmp4 < 2; fmp4++) {
163 ret = test_filename(test_urls[i], single_file, fmp4);
164 if (ret < 0) {
165 fprintf(stderr, "%s: %s\n", test_urls[i], av_err2str(ret));
166 return 1;
167 }
168 }
169 }
170 }
171 return 0;
172}
int avformat_alloc_output_context2(AVFormatContext **ctx, const AVOutputFormat *oformat, const char *format_name, const char *filename)
Allocate an AVFormatContext for an output format.
Definition mux.c:95
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition aviobuf.c:1333
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition aviobuf.c:1406
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
int main
Definition dovi_rpuenc.c:38
int ret
Definition ffplay.c:391
@ AV_CODEC_ID_WEBVTT
Definition codec_id.h:590
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition avformat.c:165
av_warn_unused_result int avformat_write_header(AVFormatContext *s, AVDictionary **options)
Allocate the stream private data and write the stream header to an output media file.
Definition mux.c:512
int av_write_trailer(AVFormatContext *s)
Write the stream trailer to an output media file and free the file private data.
Definition mux.c:1283
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_log_set_level(int level)
Set the log level.
Definition log.c:482
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define FF_ARRAY_ELEMS(a)
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1401
char * url
input or output URL.
Definition avformat.h:1518
Bytestream IO Context.
Definition avio.h:160
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:768
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:807
VariantStream * var_streams
Definition hlsenc.c:248
char * vtt_m3u8_name
Definition hlsenc.c:168
AVFormatContext * vtt_avf
Definition hlsenc.c:135
AVFormatContext * avf
Definition hlsenc.c:134
char * m3u8_name
Definition hlsenc.c:169
static int io_open(AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
Definition hlsenc.c:22
static int test_filename(const char *url, int single_file, int fmp4)
Definition hlsenc.c:34
static int io_close(AVFormatContext *s, AVIOContext *pb)
Definition hlsenc.c:28