Re: [FFmpeg-devel] [PATCH] Add mkdir support for HLS localtime-generated segment files

2016-02-17 Thread Michael Niedermayer
On Tue, Feb 16, 2016 at 09:03:52PM +0100, Johan Ström wrote:
> On 16/02/16 00:59, Michael Niedermayer wrote:
> >On Mon, Feb 15, 2016 at 10:35:57PM +0100, Johan Ström wrote:
> >>Thanks for your attention!
> >>
> >>Updated patch attached. It has been rebased against current master
> >>(avio_open2->ffio_open_whitelist changes) with no issues.
> >>
> >>On 14/02/16 01:29, Michael Niedermayer wrote:
> >>>On Wed, Feb 03, 2016 at 10:44:28PM +0100, Johan Ström wrote:
> Hi,
> 
> this patch adds -use_localtime_mkdir option to the HLS encoder.
> Use with -use_localtime, and set -hls_segment_filename to a path
> which contains a subdirectory i.e.
> /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
> This will mkdir the %Y%m%d-part of the path if it does not already exist.
> 
> In addition, each filename in the playlist output will be prefixed
> with this subdirectory (if playlist and segment shares the same base
> path).
> >>...
> >>>missing update to the docs
> >>Actually, there was no docs for the -use_localtime property at all.
> >>I've added for both use_localtime and use_localtime_mkdir now.
> >what happens if the path is not a local file but for example
> >http://...
> >
> >if that otherwise works but fails with the new option then it should
> >be documented and maybe tested for. It may seem logic for us but a
> >user might be confused why the option doesnt work for that
> That is not possible in any mode, and neither would I expect it to.
> 
> The following is with -use_localtime 1 -hls_segment_filename
> http://localhost/test/%d.ts:
> 
> [tcp @ 0x80401e880] Connection to tcp://localhost:80 failed
> (Connection refused), trying next address
> Output #0, hls, to '/storage/dvr/ipcam//test/playlist.m3u8':
>   Metadata:
> title   : RTSP Session
> encoder : Lavf57.25.100
> Stream #0:0: Video: h264, yuv420p, 352x288, q=2-31, 25 fps, 25
> tbr, 25 tbn, 25 tbc
> Stream mapping:
>   Stream #0:0 -> #0:0 (copy)
> Press [q] to stop, [?] for help
> ..
> [tcp @ 0x804065080] Connection to tcp://back-1:80 failed (Connection
> refused), trying next address
> [tcp @ 0x804065080] Connection to tcp://back-1:80 failed (Connection
> refused), trying next address
> frame=  198 fps= 29 q=-1.0 Lsize=N/A time=00:00:07.92 bitrate=N/A
> speed=1.17x
> video:350kB audio:0kB subtitle:0kB other streams:0kB global
> headers:0kB muxing overhead: unknown
> Exiting normally, received signal 2.
> 
> Same thing without -use_localtime 1.
> So, it seems it tries to use some kind of tcp transport (rather than HTTP).
> 
> I did notice a minor typo in my docs patch, so a new patch is attached.
> 
> >>>also can you add a fate test for this feature ?
> >>Sorry, I have no idea how those works or how to write one. As far as
> >>I can see there are none for HLS today which I could extend on..
> >yes, it would be great to have hls fate tests
> >without such tests someone might break hls without him noticing ...
> >but thats orthogonal to the patch
> 
> Indeed. But I'm probably not the right one to write them.
> 

>  doc/muxers.texi  |   20 
>  libavformat/hlsenc.c |   50 
> +++---
>  2 files changed, 63 insertions(+), 7 deletions(-)
> 038c27bf1fa6c5045fc0c2deb3b943c65fd194ca  
> 0001-hlsenc-add-use_localtime_mkdir-option-to-automatical.patch
> From 443289a36c71bfecbc748ad706d2fa35d3659e90 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Johan=20Str=C3=B6m?= 
> Date: Wed, 3 Feb 2016 22:20:07 +0100
> Subject: [PATCH] hlsenc: add use_localtime_mkdir option to automatically
>  create time-based directory
> 
> Use with -use_localtime, and set -hls_segment_filename to a path which
> contains a subdirectory i.e. /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
> This will mkdir the %Y%m%d-part of the path if it does not already
> exist.
> In addition, each filename in the playlist output will be prefixed with
> this subdirectory (if playlist and segment shares the same base path).
> ---
>  doc/muxers.texi  | 20 
>  libavformat/hlsenc.c | 50 +++---
>  2 files changed, 63 insertions(+), 7 deletions(-)

applied

thanks

[...]

-- 
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Many that live deserve death. And some that die deserve life. Can you give
it to them? Then do not be too eager to deal out death in judgement. For
even the very wise cannot see all ends. -- Gandalf


signature.asc
Description: Digital signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] Add mkdir support for HLS localtime-generated segment files

2016-02-16 Thread Johan Ström

On 16/02/16 00:59, Michael Niedermayer wrote:

On Mon, Feb 15, 2016 at 10:35:57PM +0100, Johan Ström wrote:

Thanks for your attention!

Updated patch attached. It has been rebased against current master
(avio_open2->ffio_open_whitelist changes) with no issues.

On 14/02/16 01:29, Michael Niedermayer wrote:

On Wed, Feb 03, 2016 at 10:44:28PM +0100, Johan Ström wrote:

Hi,

this patch adds -use_localtime_mkdir option to the HLS encoder.
Use with -use_localtime, and set -hls_segment_filename to a path
which contains a subdirectory i.e.
/some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
This will mkdir the %Y%m%d-part of the path if it does not already exist.

In addition, each filename in the playlist output will be prefixed
with this subdirectory (if playlist and segment shares the same base
path).

...

missing update to the docs

Actually, there was no docs for the -use_localtime property at all.
I've added for both use_localtime and use_localtime_mkdir now.

what happens if the path is not a local file but for example
http://...

if that otherwise works but fails with the new option then it should
be documented and maybe tested for. It may seem logic for us but a
user might be confused why the option doesnt work for that

That is not possible in any mode, and neither would I expect it to.

The following is with -use_localtime 1 -hls_segment_filename 
http://localhost/test/%d.ts:


[tcp @ 0x80401e880] Connection to tcp://localhost:80 failed (Connection 
refused), trying next address

Output #0, hls, to '/storage/dvr/ipcam//test/playlist.m3u8':
  Metadata:
title   : RTSP Session
encoder : Lavf57.25.100
Stream #0:0: Video: h264, yuv420p, 352x288, q=2-31, 25 fps, 25 tbr, 
25 tbn, 25 tbc

Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
..
[tcp @ 0x804065080] Connection to tcp://back-1:80 failed (Connection 
refused), trying next address
[tcp @ 0x804065080] Connection to tcp://back-1:80 failed (Connection 
refused), trying next address
frame=  198 fps= 29 q=-1.0 Lsize=N/A time=00:00:07.92 bitrate=N/A 
speed=1.17x
video:350kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB 
muxing overhead: unknown

Exiting normally, received signal 2.

Same thing without -use_localtime 1.
So, it seems it tries to use some kind of tcp transport (rather than HTTP).

I did notice a minor typo in my docs patch, so a new patch is attached.


also can you add a fate test for this feature ?

Sorry, I have no idea how those works or how to write one. As far as
I can see there are none for HLS today which I could extend on..

yes, it would be great to have hls fate tests
without such tests someone might break hls without him noticing ...
but thats orthogonal to the patch


Indeed. But I'm probably not the right one to write them.

From 443289a36c71bfecbc748ad706d2fa35d3659e90 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johan=20Str=C3=B6m?= 
Date: Wed, 3 Feb 2016 22:20:07 +0100
Subject: [PATCH] hlsenc: add use_localtime_mkdir option to automatically
 create time-based directory

Use with -use_localtime, and set -hls_segment_filename to a path which
contains a subdirectory i.e. /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
This will mkdir the %Y%m%d-part of the path if it does not already
exist.
In addition, each filename in the playlist output will be prefixed with
this subdirectory (if playlist and segment shares the same base path).
---
 doc/muxers.texi  | 20 
 libavformat/hlsenc.c | 50 +++---
 2 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 2e6bb4c..f2ad7fe 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -318,6 +318,26 @@ ffmpeg in.nut -hls_segment_filename 'file%03d.ts' out.m3u8
 This example will produce the playlist, @file{out.m3u8}, and segment files:
 @file{file000.ts}, @file{file001.ts}, @file{file002.ts}, etc.
 
+@item use_localtime
+Use strftime on @var{filename} to expand the segment filename with localtime.
+The segment number (%d) is not available in this mode.
+@example
+ffmpeg in.nut -use_localtime 1 -hls_segment_filename 'file-%Y%m%d-%s.ts' 
out.m3u8
+@end example
+This example will produce the playlist, @file{out.m3u8}, and segment files:
+@file{file-20160215-1455569023.ts}, @file{file-20160215-1455569024.ts}, etc.
+
+@item use_localtime_mkdir
+Used together with -use_localtime, it will create up to one subdirectory which
+is expanded in @var{filename}.
+@example
+ffmpeg in.nut -use_localtime 1 -use_localtime_mkdir 1 -hls_segment_filename 
'%Y%m%d/file-%Y%m%d-%s.ts' out.m3u8
+@end example
+This example will create a directory 201560215 (if it does not exist), and then
+produce the playlist, @file{out.m3u8}, and segment files:
+@file{201560215/file-20160215-1455569023.ts}, 
@file{201560215/file-20160215-1455569024.ts}, etc.
+
+
 @item hls_key_info_file @var{key_info_file}
 Use the information in @var{key_info_file} for segment 

Re: [FFmpeg-devel] [PATCH] Add mkdir support for HLS localtime-generated segment files

2016-02-15 Thread Michael Niedermayer
On Mon, Feb 15, 2016 at 10:35:57PM +0100, Johan Ström wrote:
> Thanks for your attention!
> 
> Updated patch attached. It has been rebased against current master
> (avio_open2->ffio_open_whitelist changes) with no issues.
> 
> On 14/02/16 01:29, Michael Niedermayer wrote:
> >On Wed, Feb 03, 2016 at 10:44:28PM +0100, Johan Ström wrote:
> >>Hi,
> >>
> >>this patch adds -use_localtime_mkdir option to the HLS encoder.
> >>Use with -use_localtime, and set -hls_segment_filename to a path
> >>which contains a subdirectory i.e.
> >>/some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
> >>This will mkdir the %Y%m%d-part of the path if it does not already exist.
> >>
> >>In addition, each filename in the playlist output will be prefixed
> >>with this subdirectory (if playlist and segment shares the same base
> >>path).
> ...
> >missing update to the docs
> Actually, there was no docs for the -use_localtime property at all.
> I've added for both use_localtime and use_localtime_mkdir now.

what happens if the path is not a local file but for example
http://...

if that otherwise works but fails with the new option then it should
be documented and maybe tested for. It may seem logic for us but a
user might be confused why the option doesnt work for that


> >>diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> >>index bc28e3c..073618d 100644
> >>--- a/libavformat/hlsenc.c
> >>+++ b/libavformat/hlsenc.c
> 
> >>+if (!tmp)
> >>+return AVERROR(ENOMEM);
> >en leaks here
> Fixed!
> >

> >also can you add a fate test for this feature ?
> Sorry, I have no idea how those works or how to write one. As far as
> I can see there are none for HLS today which I could extend on..

yes, it would be great to have hls fate tests
without such tests someone might break hls without him noticing ...
but thats orthogonal to the patch

[...]

-- 
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Breaking DRM is a little like attempting to break through a door even
though the window is wide open and the only thing in the house is a bunch
of things you dont want and which you would get tomorrow for free anyway


signature.asc
Description: Digital signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


Re: [FFmpeg-devel] [PATCH] Add mkdir support for HLS localtime-generated segment files

2016-02-15 Thread Johan Ström

Thanks for your attention!

Updated patch attached. It has been rebased against current master 
(avio_open2->ffio_open_whitelist changes) with no issues.


On 14/02/16 01:29, Michael Niedermayer wrote:

On Wed, Feb 03, 2016 at 10:44:28PM +0100, Johan Ström wrote:

Hi,

this patch adds -use_localtime_mkdir option to the HLS encoder.
Use with -use_localtime, and set -hls_segment_filename to a path
which contains a subdirectory i.e.
/some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
This will mkdir the %Y%m%d-part of the path if it does not already exist.

In addition, each filename in the playlist output will be prefixed
with this subdirectory (if playlist and segment shares the same base
path).

...

missing update to the docs
Actually, there was no docs for the -use_localtime property at all. I've 
added for both use_localtime and use_localtime_mkdir now.

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index bc28e3c..073618d 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c



+if (!tmp)
+return AVERROR(ENOMEM);

en leaks here

Fixed!


also can you add a fate test for this feature ?
Sorry, I have no idea how those works or how to write one. As far as I 
can see there are none for HLS today which I could extend on..


Regards
Johan
From 62d16f602652fc89b0578c311164eba44aae8ae5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johan=20Str=C3=B6m?= 
Date: Wed, 3 Feb 2016 22:20:07 +0100
Subject: [PATCH] hlsenc: add use_localtime_mkdir option to automatically
 create time-based directory

Use with -use_localtime, and set -hls_segment_filename to a path which
contains a subdirectory i.e. /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
This will mkdir the %Y%m%d-part of the path if it does not already
exist.
In addition, each filename in the playlist output will be prefixed with
this subdirectory (if playlist and segment shares the same base path).
---
 doc/muxers.texi  | 20 
 libavformat/hlsenc.c | 50 +++---
 2 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 2e6bb4c..2f52d2c 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -318,6 +318,26 @@ ffmpeg in.nut -hls_segment_filename 'file%03d.ts' out.m3u8
 This example will produce the playlist, @file{out.m3u8}, and segment files:
 @file{file000.ts}, @file{file001.ts}, @file{file002.ts}, etc.
 
+@item use_localtime
+Use strftime on @var{filename} to expand the segment filename with localtime.
+The segment number (%d) is not available in this mode.
+@example
+ffmpeg in.nut -use_localtime -hls_segment_filename 'file-%Y%m%d-%s.ts' out.m3u8
+@end example
+This example will produce the playlist, @file{out.m3u8}, and segment files:
+@file{file-20160215-1455569023.ts}, @file{file-20160215-1455569024.ts}, etc.
+
+@item use_localtime_mkdir
+Used together with -use_localtime, it will create up to one subdirectory which
+is expanded in @var{filename}.
+@example
+ffmpeg in.nut -use_localtime -use_localtime_mkdir -hls_segment_filename 
'%Y%m%d/file-%Y%m%d-%s.ts' out.m3u8
+@end example
+This example will create a directory 201560215 (if it does not exist), and then
+produce the playlist, @file{out.m3u8}, and segment files:
+@file{201560215/file-20160215-1455569023.ts}, 
@file{201560215/file-20160215-1455569024.ts}, etc.
+
+
 @item hls_key_info_file @var{key_info_file}
 Use the information in @var{key_info_file} for segment encryption. The first
 line of @var{key_info_file} specifies the key URI written to the playlist. The
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 7ab7cbb..8c61b6d 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -82,6 +82,7 @@ typedef struct HLSContext {
 char *segment_filename;
 
 int use_localtime;  ///< flag to expand filename with localtime
+int use_localtime_mkdir;///< flag to mkdir dirname in timebased filename
 int allowcache;
 int64_t recording_time;
 int has_video;
@@ -304,16 +305,35 @@ static int hls_mux_init(AVFormatContext *s)
 }
 
 /* Create a new segment and append it to the segment list */
-static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
-  int64_t size)
+static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, 
double duration,
+  int64_t pos, int64_t size)
 {
 HLSSegment *en = av_malloc(sizeof(*en));
+char *tmp, *p;
+const char *pl_dir, *filename;
 int ret;
 
 if (!en)
 return AVERROR(ENOMEM);
 
-av_strlcpy(en->filename, av_basename(hls->avf->filename), 
sizeof(en->filename));
+filename = av_basename(hls->avf->filename);
+
+if (hls->use_localtime_mkdir) {
+/* Possibly prefix with mkdir'ed subdir, if playlist share same
+ * base path. */
+tmp = av_strdup(s->filename);
+if (!tmp) {
+av_free(en);
+return AVERROR(ENOMEM);
+}
+
+  

Re: [FFmpeg-devel] [PATCH] Add mkdir support for HLS localtime-generated segment files

2016-02-13 Thread Michael Niedermayer
On Wed, Feb 03, 2016 at 10:44:28PM +0100, Johan Ström wrote:
> Hi,
> 
> this patch adds -use_localtime_mkdir option to the HLS encoder.
> Use with -use_localtime, and set -hls_segment_filename to a path
> which contains a subdirectory i.e.
> /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
> This will mkdir the %Y%m%d-part of the path if it does not already exist.
> 
> In addition, each filename in the playlist output will be prefixed
> with this subdirectory (if playlist and segment shares the same base
> path).
> 
> Example:
> 
> $ ffmpeg  -f hls -use_localtime 1 -use_localtime_mkdir 1
> -hls_segment_filename /dvr/test/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
> /dvr/test/playlist.m3u8
> 
> This will create put segments created (today) in
> /dvr/test/20160203/, and the 20160203 dir will be automatically
> created if it does not already exist.
> The m3u8 playlist will contain segment paths like this:
> 
> 20160203/20160203T224121-1454535681.ts
> 
> The subdir support could possibly be enabled outside of the
> localtime/mkdir-scope.
> 
> 
> Regards
> Johan
> 

>  hlsenc.c |   46 --
>  1 file changed, 40 insertions(+), 6 deletions(-)
> f3bbd115d143c8dd40fef92eb4a84b6c4ef574e7  
> 0001-hlsenc-add-use_localtime_mkdir-option-to-automatical.patch
> From 40327f78d70b3f1b97b222de5adf70020c466db9 Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Johan=20Str=C3=B6m?= 
> Date: Wed, 3 Feb 2016 22:20:07 +0100
> Subject: [PATCH] hlsenc: add use_localtime_mkdir option to automatically
>  create time-based directory
> 
> Use with -use_localtime, and set -hls_segment_filename to a path which
> contains a subdirectory i.e. /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
> This will mkdir the %Y%m%d-part of the path if it does not already
> exist.
> In addition, each filename in the playlist output will be prefixed with
> this subdirectory (if playlist and segment shares the same base path).
> ---
>  libavformat/hlsenc.c | 46 --
>  1 file changed, 40 insertions(+), 6 deletions(-)

missing update to the docs


> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index bc28e3c..073618d 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -82,6 +82,7 @@ typedef struct HLSContext {
>  char *segment_filename;
>  
>  int use_localtime;  ///< flag to expand filename with localtime
> +int use_localtime_mkdir;///< flag to mkdir dirname in timebased filename
>  int allowcache;
>  int64_t recording_time;
>  int has_video;
> @@ -303,16 +304,33 @@ static int hls_mux_init(AVFormatContext *s)
>  }
>  
>  /* Create a new segment and append it to the segment list */
> -static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
> -  int64_t size)
> +static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, 
> double duration,
> +  int64_t pos, int64_t size)
>  {
>  HLSSegment *en = av_malloc(sizeof(*en));
> +char *tmp, *p;
> +const char *pl_dir, *filename;
>  int ret;
>  
>  if (!en)
>  return AVERROR(ENOMEM);
>  
> -av_strlcpy(en->filename, av_basename(hls->avf->filename), 
> sizeof(en->filename));
> +filename = av_basename(hls->avf->filename);
> +
> +if (hls->use_localtime_mkdir) {
> +/* Possibly prefix with mkdir'ed subdir, if playlist share same
> + * base path. */
> +tmp = av_strdup(s->filename);

> +if (!tmp)
> +return AVERROR(ENOMEM);

en leaks here

also can you add a fate test for this feature ?

[...]

-- 
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Asymptotically faster algorithms should always be preferred if you have
asymptotical amounts of data


signature.asc
Description: Digital signature
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


[FFmpeg-devel] [PATCH] Add mkdir support for HLS localtime-generated segment files

2016-02-03 Thread Johan Ström

Hi,

this patch adds -use_localtime_mkdir option to the HLS encoder.
Use with -use_localtime, and set -hls_segment_filename to a path which 
contains a subdirectory i.e. /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts

This will mkdir the %Y%m%d-part of the path if it does not already exist.

In addition, each filename in the playlist output will be prefixed with 
this subdirectory (if playlist and segment shares the same base path).


Example:

$ ffmpeg  -f hls -use_localtime 1 -use_localtime_mkdir 1 
-hls_segment_filename /dvr/test/%Y%m%d/%Y%m%dT%H%M%S-%s.ts 
/dvr/test/playlist.m3u8


This will create put segments created (today) in /dvr/test/20160203/, 
and the 20160203 dir will be automatically created if it does not 
already exist.

The m3u8 playlist will contain segment paths like this:

20160203/20160203T224121-1454535681.ts

The subdir support could possibly be enabled outside of the 
localtime/mkdir-scope.



Regards
Johan

From 40327f78d70b3f1b97b222de5adf70020c466db9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johan=20Str=C3=B6m?= 
Date: Wed, 3 Feb 2016 22:20:07 +0100
Subject: [PATCH] hlsenc: add use_localtime_mkdir option to automatically
 create time-based directory

Use with -use_localtime, and set -hls_segment_filename to a path which
contains a subdirectory i.e. /some/path/%Y%m%d/%Y%m%dT%H%M%S-%s.ts
This will mkdir the %Y%m%d-part of the path if it does not already
exist.
In addition, each filename in the playlist output will be prefixed with
this subdirectory (if playlist and segment shares the same base path).
---
 libavformat/hlsenc.c | 46 --
 1 file changed, 40 insertions(+), 6 deletions(-)

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index bc28e3c..073618d 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -82,6 +82,7 @@ typedef struct HLSContext {
 char *segment_filename;
 
 int use_localtime;  ///< flag to expand filename with localtime
+int use_localtime_mkdir;///< flag to mkdir dirname in timebased filename
 int allowcache;
 int64_t recording_time;
 int has_video;
@@ -303,16 +304,33 @@ static int hls_mux_init(AVFormatContext *s)
 }
 
 /* Create a new segment and append it to the segment list */
-static int hls_append_segment(HLSContext *hls, double duration, int64_t pos,
-  int64_t size)
+static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, 
double duration,
+  int64_t pos, int64_t size)
 {
 HLSSegment *en = av_malloc(sizeof(*en));
+char *tmp, *p;
+const char *pl_dir, *filename;
 int ret;
 
 if (!en)
 return AVERROR(ENOMEM);
 
-av_strlcpy(en->filename, av_basename(hls->avf->filename), 
sizeof(en->filename));
+filename = av_basename(hls->avf->filename);
+
+if (hls->use_localtime_mkdir) {
+/* Possibly prefix with mkdir'ed subdir, if playlist share same
+ * base path. */
+tmp = av_strdup(s->filename);
+if (!tmp)
+return AVERROR(ENOMEM);
+
+pl_dir = av_dirname(tmp);
+p = hls->avf->filename;
+if (strstr(p, pl_dir) == p)
+filename = hls->avf->filename + strlen(pl_dir) + 1;
+av_free(tmp);
+}
+av_strlcpy(en->filename, filename, sizeof(en->filename));
 
 if(hls->has_subtitle)
 av_strlcpy(en->sub_filename, av_basename(hls->vtt_avf->filename), 
sizeof(en->sub_filename));
@@ -509,6 +527,21 @@ static int hls_start(AVFormatContext *s)
 av_log(oc, AV_LOG_ERROR, "Could not get segment filename with 
use_localtime\n");
 return AVERROR(EINVAL);
 }
+
+if (c->use_localtime_mkdir) {
+const char *dir;
+char *fn_copy = av_strdup(oc->filename);
+if (!fn_copy) {
+return AVERROR(ENOMEM);
+}
+dir = av_dirname(fn_copy);
+if (mkdir(dir, 0777) == -1 && errno != EEXIST) {
+av_log(oc, AV_LOG_ERROR, "Could not create directory %s 
with use_localtime_mkdir\n", dir);
+av_free(fn_copy);
+return AVERROR(errno);
+}
+av_free(fn_copy);
+}
} else if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
   c->basename, c->wrap ? c->sequence % c->wrap 
: c->sequence) < 0) {
 av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s' 
you can try use -use_localtime 1 with it\n", c->basename);
@@ -782,7 +815,7 @@ static int hls_write_packet(AVFormatContext *s, AVPacket 
*pkt)
 
 new_start_pos = avio_tell(hls->avf->pb);
 hls->size = new_start_pos - hls->start_pos;
-ret = hls_append_segment(hls, hls->duration, hls->start_pos, 
hls->size);
+ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, 
hls->size);