typedef struct HLSContext {
const AVClass *class; // Class for private options.
- int number;
+ unsigned number;
+ int64_t sequence;
AVOutputFormat *oformat;
AVFormatContext *avf;
float time; // Set by a private option.
int has_video;
int64_t start_pts;
int64_t end_pts;
+ int64_t duration; ///< last segment duration computed so far, in seconds
+ int nb_entries;
ListEntry *list;
ListEntry *end_list;
char *basename;
if (!en)
return AVERROR(ENOMEM);
- av_get_frame_filename(en->name, sizeof(en->name), hls->basename,
- hls->number -1);
+ av_strlcpy(en->name, av_basename(hls->avf->filename), sizeof(en->name));
en->duration = duration;
en->next = NULL;
hls->end_list = en;
- if (hls->number >= hls->size) {
+ if (hls->nb_entries >= hls->size) {
en = hls->list;
hls->list = en->next;
av_free(en);
- }
+ } else
+ hls->nb_entries++;
+
+ hls->sequence++;
return 0;
}
{
HLSContext *hls = s->priv_data;
ListEntry *en;
+ int target_duration = 0;
int ret = 0;
if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
&s->interrupt_callback, NULL)) < 0)
goto fail;
+ for (en = hls->list; en; en = en->next) {
+ if (target_duration < en->duration)
+ target_duration = en->duration;
+ }
+
avio_printf(hls->pb, "#EXTM3U\n");
avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
- avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time);
- avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
- FFMAX(0, hls->number - hls->size));
+ avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", target_duration);
+ avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%"PRId64"\n",
+ FFMAX(0, hls->sequence - hls->size));
for (en = hls->list; en; en = en->next) {
avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
hls->number = 0;
- hls->recording_time = hls->time * 1000000;
+ hls->recording_time = hls->time * AV_TIME_BASE;
hls->start_pts = AV_NOPTS_VALUE;
for (i = 0; i < s->nb_streams; i++)
AVFormatContext *oc = hls->avf;
AVStream *st = s->streams[pkt->stream_index];
int64_t end_pts = hls->recording_time * hls->number;
- int ret;
+ int ret, is_ref_pkt = 0;
if (hls->start_pts == AV_NOPTS_VALUE) {
hls->start_pts = pkt->pts;
hls->end_pts = pkt->pts;
}
- end_pts += hls->start_pts;
if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
- av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
+ pkt->pts != AV_NOPTS_VALUE) {
+ is_ref_pkt = 1;
+ hls->duration = av_rescale(pkt->pts - hls->end_pts,
+ st->time_base.num, st->time_base.den);
+ }
+
+ if (is_ref_pkt &&
+ av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
+ end_pts, AV_TIME_BASE_Q) >= 0 &&
pkt->flags & AV_PKT_FLAG_KEY) {
- append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
- st->time_base.num,
- st->time_base.den));
+ ret = append_entry(hls, hls->duration);
+ if (ret)
+ return ret;
+
hls->end_pts = pkt->pts;
+ hls->duration = 0;
av_write_frame(oc, NULL); /* Flush any buffered data */
avio_close(oc->pb);
avio_closep(&oc->pb);
avformat_free_context(oc);
av_free(hls->basename);
+ append_entry(hls, hls->duration);
hls_window(s, 1);
free_entries(hls);
#define OFFSET(x) offsetof(HLSContext, x)
#define E AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = {
- { "start_number", "set first number in the sequence", OFFSET(number), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
- { "hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
- { "hls_list_size","set maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
- { "hls_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
+ {"start_number", "set first number in the sequence", OFFSET(sequence),AV_OPT_TYPE_INT64, {.i64 = 0}, 0, INT64_MAX, E},
+ {"hls_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E},
+ {"hls_list_size", "set maximum number of playlist entries", OFFSET(size), AV_OPT_TYPE_INT, {.i64 = 5}, 0, INT_MAX, E},
+ {"hls_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E},
{ NULL },
};