[FFmpeg-devel] [PATCH] delete the old segment file from hls list

Stefano Sabatini stefasab at gmail.com
Thu Aug 14 12:25:34 CEST 2014


On date Monday 2014-08-11 17:39:37 +0800, Steven Liu encoded:
> Hi Guys,
> 
>      The FFmpeg hls module can make m3u8 and ts, but it dosen't delete the
> old ts segment file.
>       If always run this module, the disk will full, so this patch can fix
> the problem.
>       When update the segment list m3u8 file, it will delete the ts segment
> out range from the list file.
> 
>      before use this patch:
> [root at localhost ffmpeg]# ls *.ts *.m3u8
> a0.ts  a10.ts  a11.ts  a12.ts  a13.ts  a14.ts  a15.ts  a16.ts  a17.ts
> a18.ts  a19.ts  a1.ts  a20.ts  a2.ts  a3.ts  a4.ts  a5.ts  a6.ts  a7.ts
> a8.ts  a9.ts  a.m3u8
> [root at localhost ffmpeg]# cat a.m3u8
> #EXTM3U
> #EXT-X-VERSION:3
> #EXT-X-TARGETDURATION:11
> #EXT-X-MEDIA-SEQUENCE:16
> #EXTINF:10.427075,
> a16.ts
> #EXTINF:10.427075,
> a17.ts
> #EXTINF:10.427075,
> a18.ts
> #EXTINF:10.427075,
> a19.ts
> #EXTINF:3.670330,
> a20.ts
> #EXT-X-ENDLIST
> [root at localhost ffmpeg]#
> 
> after use this patch:
> [root at localhost ffmpeg]# ls *.ts *.m3u8
> a10.ts  a11.ts  a12.ts  a13.ts  a9.ts  a.m3u8
> [root at localhost ffmpeg]# cat a.m3u8
> #EXTM3U
> #EXT-X-VERSION:3
> #EXT-X-TARGETDURATION:11
> #EXT-X-MEDIA-SEQUENCE:9
> #EXTINF:10.427075,
> a9.ts
> #EXTINF:10.427075,
> a10.ts
> #EXTINF:10.427075,
> a11.ts
> #EXTINF:10.427075,
> a12.ts
> #EXTINF:2.335665,
> a13.ts
> #EXT-X-ENDLIST
> [root at localhost ffmpeg]#
> 
> -------------------------------------------
> The patch context:
> 
> when update the hls m3u8 list, the old file is not unlinked
> this patch can do this operation
> 
> Signed-off-by: Steven Liu <qi.liu at chinacache.com>
> ---
>  libavformat/hlsenc.c |    5 +++++
>  1 files changed, 5 insertions(+), 0 deletions(-)
> 
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index 11f1e5b..2ee0970 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -30,6 +30,10 @@
> 
>  #include "avformat.h"
>  #include "internal.h"
> +#if HAVE_UNISTD_H
> +#include <unistd.h>
> +#endif
> +
> 
>  typedef struct HLSSegment {
>      char filename[1024];
> @@ -115,6 +119,7 @@ static int hls_append_segment(HLSContext *hls, double
> duration)
>      if (hls->max_nb_segments && hls->nb_entries >= hls->max_nb_segments) {
>          en = hls->segments;
>          hls->segments = en->next;
> +        unlink(en->filename);

Check for the return value, and log an explicit error message in case
of failure.

>          av_free(en);
>      } else
>          hls->nb_entries++;

Also, in order not to alter the standard behavior, I think it might be
safer to add an option to enable this behavior. Also, what happens if
unistd.h is not available?
-- 
FFmpeg = Frightening and Forgiving Monstrous Proud Elastic Gadget


More information about the ffmpeg-devel mailing list