From 1a8906a4810a0136d1ed540e2614779b0e26132c Mon Sep 17 00:00:00 2001 From: CoolnsX Date: Thu, 21 Jul 2022 19:10:04 +0530 Subject: [PATCH] feat:subtitles support --- README.md | 5 ++++- hls | 24 +++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d7f7b8b..dec9421 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,14 @@ Options: -r select highest resolution automatically -n set maximum number of connections (default : 36) -f skip ffmpeg file conversion (used to enable the video file to run on any video player) + -s subtitles url or path + +Note: if subtitles url is passed using [-s] along with skip ffmpeg [-f] then the script will download subtitle file with same name instead of burning it in video file ``` # Increase/Decrease Parallel Downloads.. -Currently its set to 36 in [line 23](https://github.com/CoolnsX/hls_downloader/blob/main/hls#L23) in script +Currently its set to 36 in [line 26](https://github.com/CoolnsX/hls_downloader/blob/main/hls#L26) in script You can Increase/Decrease it by using ```-n ``` ``` diff --git a/hls b/hls index 69c0046..c13af76 100755 --- a/hls +++ b/hls @@ -16,6 +16,9 @@ help_text () { -r select highest resolution automatically -n set maximum number of connections (default : 36) -f skip ffmpeg file conversion (used to enable the video file to run on any video player) + -s subtitles url or path + + Note: if subtitles url is passed using [-s] along with skip ffmpeg [-f] is then the script will download subtitle file with same name instead of burning it in video file EOF } @@ -25,12 +28,13 @@ file="video" tmpdir="${XDG_CACHE_HOME:-$HOME/.cache}/hls-temp" jobdir="${XDG_CACHE_HOME:-$HOME/.cache}/hls-jobs" -while getopts 'o:rfhn:' OPT; do +while getopts 'o:rfhn:s:' OPT; do case $OPT in o) file=$OPTARG ;; n) n=$OPTARG ;; f) skip_ffmpeg=1;; r) skip_res=1;; + s) subs=$OPTARG;; *|h) help_text exit 0 @@ -64,11 +68,7 @@ fi key_uri="$(printf "%s" "$resp" | sed -nE 's/^#EXT-X-KEY.*URI="([^"]*)"/\1/p')" [ -z "$key_uri" ] || iv_uri="$(printf "%s" "$resp" | sed -nE 's/^#EXT-X-IV.*URI="([^"]*)"/\1/p')" data="$(printf "%s" "$resp" | sed '/#/d')" -if printf "%s" "$data" | grep -q "http";then - relative_url='' -else - relative_url=$(printf "%s" "$url" | sed 's_[^/]*$__') -fi +printf "%s" "$data" | grep -q "http" && relative_url='' || relative_url=$(printf "%s" "$url" | sed 's_[^/]*$__') range=$(printf "%s\n" "$data" | wc -l) #for encrypted stream only @@ -82,7 +82,7 @@ fi printf "\033[2K\r\033[1;35mpieces : $range\n\033[1;33mDownloading.." #downloading .ts data asynchronously for i in $(seq $range); do - curl -s "${relative_url}$(printf "%s" "$data" | sed -n "${i}p")" > "$tmpdir/$(printf "%04d" "$i").ts" && printf "\033[2K\r\033[1;32m ✓ $i / $range done" & + curl -s "${relative_url}$(printf "%s" "$data" | sed -n "${i}p")" > "$tmpdir/$(printf "%05d" "$i").ts" && printf "\033[2K\r\033[1;32m ✓ $i / $range done" & jobs -p > "$jobdir" while [ "$(cat "$jobdir" | wc -l)" -ge $n ];do jobs > "$jobdir";sleep 0.05;done done @@ -93,7 +93,7 @@ if [ -n "$key_uri" ];then #decrypting while concatenating.. printf "\033[2K\r\033[1;36m Decrypting and Concatenating pieces into single file.." for i in "$tmpdir"/*;do - cat "$i" | openssl aes-128-cbc -d -K "$key" -iv "$iv" -nopad >> "$file.ts" + openssl aes-128-cbc -d -K "$key" -iv "$iv" -nopad >> "$file.ts" < "$i" done else printf "\033[2K\r\033[1;36m Concatenating pieces into single file.." @@ -102,7 +102,13 @@ fi rm -rdf $tmpdir $jobdir #conversion of allts file to mp4 video using ffmpeg.. -[ -z "$skip_ffmpeg" ] && printf "\033[2K\r\033[1;36mEncoding file to mp4 video..\n\033[0m" && ffmpeg -i "$file.ts" -loglevel error -stats -c copy "$file.mp4" || mv "$file.ts" "$file.mp4" +if [ -z "$skip_ffmpeg" ];then + printf "\033[2K\r\033[1;36mEncoding file to mp4 video..\n\033[0m" + [ -z "$subs" ] && ffmpeg -i "$file.ts" -loglevel error -stats -c copy "$file.mp4" || ffmpeg -i "$file.ts" -i "$subs" -loglevel error -stats -c copy -c:s mov_text "$file.mp4" +else + mv "$file.ts" "$file.mp4" + [ -z "$subs" ] || curl -s "$subs" -o "$file.srt" +fi #cleanup.. rm -f "$file".ts