From patchwork Tue Dec 26 11:17:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Dixit, Vishwanath" X-Patchwork-Id: 6986 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.79.195 with SMTP id r64csp6729232jad; Tue, 26 Dec 2017 03:17:45 -0800 (PST) X-Google-Smtp-Source: ACJfBovf2vrvXaaIyg9nLKcKcLqYzqIPdjMWrFnw6+zsSUxUvcv1/DatwjJQHOO399wJlrNUpjIy X-Received: by 10.223.155.131 with SMTP id d3mr27310952wrc.29.1514287065883; Tue, 26 Dec 2017 03:17:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514287065; cv=none; d=google.com; s=arc-20160816; b=PlpLuQ7WhnOULaaopKWEm9pspY3OCuoOijy8G0YLkCmiNZtIYvVARDghddzHxJWOPB p+n594vY/PIlExh9mB1AADKx/3+YZhw7dULs8v6I64k10dEKq0uDgSz5kIT5B/ly60bJ 6kRzL3jGiDAer9CigrAaFuZI2Lh5vKxpZW9Rk5lkz01Haf236TIggRfJilSFTaFPJ1jk qamROTOWrjmwT9g8ELRly08PnRQ9kdsQ7a7NBCN+az+zp5oetuCE1zHNgFKrW85e1+xA l8P49bDaPDD9GdVjOo7MXDPaHMeIcCC9KlaG4E/2JdwRbZPMbOpPmTOflyMHLFSLyusT NJZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:feedback-id:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to :arc-authentication-results; bh=j9gzrdo22ascvZZCmhGVKgdztAsApiMwZotaFmU2xFw=; b=W2/5c07fJqASxHVblgjOpEhqX+TJKq2eVbbaxCFg6rUjkVtwm7V8qAH0lHRrKVGLsS YAlwRFkqE7ZVgffPxNYZm491UgUYUWclDLJc/we/Xm1pfSTQtH9Rv/iHPO3a3QS4DuI3 f+NqENHuCf/OZEeWct7Vc9ii+9AH+zr/mQHdWNnmbZP0tlrAxeTdtN5q6WqaqueBWJKs /FXs6lSgM2ambtVREg+xUGSX8BCjo9LSaF1YpxnEdtSTXZ5mPzB6vba5uvgWa8wxAhY4 LQKBjO+xcU9ThfHm7bF+oTfk/lRkhXL8xs+2oHubTJ3CIn3izOvnsPmO5t5V9FGFFOCh y/VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@smtpservice.net header.s=m78bu0.a1-4.dyn header.b=FGq5Sm4x; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=akamai.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id m19si11830822wma.25.2017.12.26.03.17.45; Tue, 26 Dec 2017 03:17:45 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@smtpservice.net header.s=m78bu0.a1-4.dyn header.b=FGq5Sm4x; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=NONE dis=NONE) header.from=akamai.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E0FBA688346; Tue, 26 Dec 2017 13:17:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from a2i831.smtp2go.com (a2i831.smtp2go.com [103.47.207.63]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 92CC068831F for ; Tue, 26 Dec 2017 13:17:23 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpservice.net; s=m78bu0.a1-4.dyn; x=1514287962; h=Feedback-ID: X-Smtpcorp-Track:Message-Id:Date:Subject:To:From:Reply-To:Sender: List-Unsubscribe; bh=Scy1lIPTb0WMrKBqx8QbzG2fE/5liKlxC+rgMXJTBwg=; b=FGq5Sm4x p2lFFIK0WZ/yRk9UMakrJbTTsK78JWku7TqmFUTJdR+7dPKis3Xc2gyqbx1lHXtS0P2zKiTgWbUqf ZiLWjczkSVea2ybThMQExLLnvhraUuleOlbjbTA6RhttQBzK8sBA7kis+E0VebYRVjNS/EUCkBGEx QJ3g3THPl5NVQwxUgwr0F/4AHujdBfBVR55izV08CY+NbtBy5tKWsFPWEB0pCLZkWMFddBR6jg1NY QL+vgQT+6y5In9IXlXmluNvEcVuNuXYTeBXl1nhMh2qeWCfXhBohHFeaWhUEXNUSIT5X6YfXGcSDA r3gqPHnrf2nflSb7Du9YSf8jVg==; From: vdixit@akamai.com To: ffmpeg-devel@ffmpeg.org Date: Tue, 26 Dec 2017 16:47:29 +0530 Message-Id: <1514287049-22043-1-git-send-email-vdixit@akamai.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1514282029-7447-1-git-send-email-vdixit@akamai.com> References: <1514282029-7447-1-git-send-email-vdixit@akamai.com> X-Smtpcorp-Track: 1-TnEkDIIg05DF.QXBe06YRE Feedback-ID: 337386m:337386asVRLGB:337386s0Snv8KWO-:SMTPCORP X-Report-Abuse: Please forward a copy of this message, including all headers, to Subject: [FFmpeg-devel] [PATCH v3 3/3] avformat/hlsenc: creation of variant streams in subdirectories X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Vishwanath Dixit MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Vishwanath Dixit --- doc/muxers.texi | 33 ++++++++++++++++++++++++- libavformat/hlsenc.c | 68 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/doc/muxers.texi b/doc/muxers.texi index 6af970d..2951262 100644 --- a/doc/muxers.texi +++ b/doc/muxers.texi @@ -587,6 +587,20 @@ This example will produce the playlists segment file sets: @file{file_0_000.ts}, @file{file_0_001.ts}, @file{file_0_002.ts}, etc. and @file{file_1_000.ts}, @file{file_1_001.ts}, @file{file_1_002.ts}, etc. +The string "%v" may be present in the filename or in the last directory name +containing the file. If the string is present in the directory name, then +sub-directories are created after expanding the directory name pattern. This +enables creation of segments corresponding to different variant streams in +subdirectories. +@example +ffmpeg -i in.ts -b:v:0 1000k -b:v:1 256k -b:a:0 64k -b:a:1 32k \ + -map 0:v -map 0:a -map 0:v -map 0:a -f hls -var_stream_map "v:0,a:0 v:1,a:1" \ + -hls_segment_filename 'vs%v/file_%03d.ts' vs%v/out.m3u8 +@end example +This example will produce the playlists segment file sets: +@file{vs0/file_000.ts}, @file{vs0/file_001.ts}, @file{vs0/file_002.ts}, etc. and +@file{vs1/file_000.ts}, @file{vs1/file_001.ts}, @file{vs1/file_002.ts}, etc. + @item use_localtime Use strftime() on @var{filename} to expand the segment filename with localtime. The segment number is also available in this mode, but to use it, you need to specify second_level_segment_index @@ -715,6 +729,11 @@ set filename to the fragment files header file, default filename is @file{init.m When @code{var_stream_map} is set with two or more variant streams, the @var{filename} pattern must contain the string "%v", this string specifies the position of variant stream index in the generated init file names. +The string "%v" may be present in the filename or in the last directory name +containing the file. If the string is present in the directory name, then +sub-directories are created after expanding the directory name pattern. This +enables creation of init files corresponding to different variant streams in +subdirectories. @item hls_flags @var{flags} Possible values: @@ -831,7 +850,11 @@ Allowed values are 0 to 9 (limited just based on practical usage). When there are two or more variant streams, the output filename pattern must contain the string "%v", this string specifies the position of variant stream -index in the output media playlist filenames. +index in the output media playlist filenames. The string "%v" may be present in +the filename or in the last directory name containing the file. If the string is +present in the directory name, then sub-directories are created after expanding +the directory name pattern. This enables creation of variant streams in +subdirectories. @example ffmpeg -re -i in.ts -b:v:0 1000k -b:v:1 256k -b:a:0 64k -b:a:1 32k \ @@ -854,6 +877,14 @@ be an audio only stream with bitrate 64k and the third variant stream will be a video only stream with bitrate 256k. Here, three media playlist with file names out_0.m3u8, out_1.m3u8 and out_2.m3u8 will be created. @example +ffmpeg -re -i in.ts -b:v:0 1000k -b:v:1 256k -b:a:0 64k -b:a:1 32k \ + -map 0:v -map 0:a -map 0:v -map 0:a -f hls -var_stream_map "v:0,a:0 v:1,a:1" \ + http://example.com/live/vs_%v/out.m3u8 +@end example +This example creates the variant streams in subdirectories. Here, the first +media playlist is created at @file{http://example.com/live/vs_0/out.m3u8} and +the second one at @file{http://example.com/live/vs_1/out.m3u8}. +@example ffmpeg -re -i in.ts -b:a:0 32k -b:a:1 64k -b:v:0 1000k -b:v:1 3000k \ -map 0:a -map 0:a -map 0:v -map 0:v -f hls \ -var_stream_map "a:0,agroup:aud_low a:1,agroup:aud_high v:0,agroup:aud_low v:1,agroup:aud_high" \ diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index 198c9d3..b25bfc9 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -1557,7 +1557,8 @@ static int append_postfix(char *name, int name_buf_len, int i) static int validate_name(int nb_vs, const char *fn) { - const char *filename; + const char *filename, *subdir_name; + char *fn_dup = NULL; int ret = 0; if (!fn) { @@ -1565,22 +1566,38 @@ static int validate_name(int nb_vs, const char *fn) goto fail; } + fn_dup = av_strdup(fn); + if (!fn_dup) { + ret = AVERROR(ENOMEM); + goto fail; + } + filename = av_basename(fn); + subdir_name = av_dirname(fn_dup); - if (nb_vs > 1 && !av_stristr(filename, "%v")) { + if (nb_vs > 1 && !av_stristr(filename, "%v") && !av_stristr(subdir_name, "%v")) { av_log(NULL, AV_LOG_ERROR, "More than 1 variant streams are present, %%v is expected in the filename %s\n", fn); ret = AVERROR(EINVAL); goto fail; } + if (av_stristr(filename, "%v") && av_stristr(subdir_name, "%v")) { + av_log(NULL, AV_LOG_ERROR, "%%v is expected either in filename or in the sub-directory name of file %s\n", + fn); + ret = AVERROR(EINVAL); + goto fail; + } + fail: + av_freep(&fn_dup); return ret; } static int format_name(char *buf, int buf_len, int index) { - char *orig_buf_dup = NULL; + const char *proto, *dir; + char *orig_buf_dup = NULL, *mod_buf_dup = NULL; int ret = 0; if (!av_stristr(buf, "%v")) @@ -1597,8 +1614,27 @@ static int format_name(char *buf, int buf_len, int index) goto fail; } + proto = avio_find_protocol_name(orig_buf_dup); + dir = av_dirname(orig_buf_dup); + + /* if %v is present in the file's directory, create sub-directory */ + if (av_stristr(dir, "%v") && proto && !strcmp(proto, "file")) { + mod_buf_dup = av_strdup(buf); + if (!mod_buf_dup) { + ret = AVERROR(ENOMEM); + goto fail; + } + + dir = av_dirname(mod_buf_dup); + if (mkdir_p(dir) == -1 && errno != EEXIST) { + ret = AVERROR(errno); + goto fail; + } + } + fail: av_freep(&orig_buf_dup); + av_freep(&mod_buf_dup); return ret; } @@ -1745,16 +1781,30 @@ static int update_variant_stream_info(AVFormatContext *s) { static int update_master_pl_info(AVFormatContext *s) { HLSContext *hls = s->priv_data; const char *dir; - char *fn = NULL; + char *fn1= NULL, *fn2 = NULL; int ret = 0; - fn = av_strdup(s->filename); - if (!fn) { + fn1 = av_strdup(s->filename); + if (!fn1) { ret = AVERROR(ENOMEM); goto fail; } - dir = av_dirname(fn); + dir = av_dirname(fn1); + + /** + * if output file's directory has %v, variants are created in sub-directories + * then master is created at the sub-directories level + */ + if (dir && av_stristr(av_basename(dir), "%v")) { + fn2 = av_strdup(dir); + if (!fn2) { + ret = AVERROR(ENOMEM); + goto fail; + } + dir = av_dirname(fn2); + } + if (dir && strcmp(dir, ".")) hls->master_m3u8_url = av_append_path_component(dir, hls->master_pl_name); else @@ -1766,7 +1816,9 @@ static int update_master_pl_info(AVFormatContext *s) { } fail: - av_freep(&fn); + av_freep(&fn1); + av_freep(&fn2); + return ret; }