feat:minor changes

This commit is contained in:
CoolnsX
2022-07-21 15:59:53 +05:30
parent 1ced6fc23c
commit fe5802449d
2 changed files with 49 additions and 24 deletions

View File

@@ -1,15 +1,29 @@
# hls_downloader # hls_downloader
A posix compliant highly fast and efficient Asynchronous stable m3u8 links dowloader that uses shell jobs for controlling parallel download... A posix compliant highly fast and efficient Asynchronous stable m3u8 links parallel downloader that uses shell jobs for controlling parallel download...
# Increase Parallel Downloads.. ```
Usage:
hls [ -o <filename> ] [ -r | -f ] [ <m3u8_link> ]
hls -h
Currently its set to my internet speed * 3 in [line 72](https://github.com/CoolnsX/hls_downloader/blob/main/hls#L72) in script Options:
-h show helptext
-o filename (default : video)
-r select highest resolution automatically
-f skip ffmpeg file conversion (used to enable the video file to run on any video player)
```
# Increase/Decrease Parallel Downloads..
Currently its set to 36 in [line 23](https://github.com/CoolnsX/hls_downloader/blob/main/hls#L23) in script
You can Increase/Decrease it by using ```-n <no. of connections>```
``` ```
Internet Speed = 12 MByte per seconds.. Internet Speed = 12 MByte per seconds..
36 (Internet Speed * 3).. 36 (Internet Speed * 3)..
``` ```
NOTE :- Increasing the number will make the download faster but less stable and Decreasing the number will make download slower but stable.. Decrease the number if the script is hanging out during download process..
# Dependency # Dependency

37
hls
View File

@@ -7,25 +7,30 @@ help_text () {
printf "%s\n" "$line" printf "%s\n" "$line"
done <<-EOF done <<-EOF
Usage: Usage:
${0##*/} [ -f <filename> ] [ -s ] [ <m3u8_link> ] ${0##*/} [ -o <filename> ] [ -r | -f | -n <no. of connections>] [ <m3u8_link> ]
${0##*/} -h ${0##*/} -h
Options: Options:
-h show helptext -h show helptext
-f filename (default : video) -o filename (default : video)
-s select highest resolution automatically -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)
EOF EOF
} }
skip_res=0 skip_res=0
n=36
file="video" file="video"
tmpdir="${XDG_CACHE_HOME:-$HOME/.cache}/hls-temp" tmpdir="${XDG_CACHE_HOME:-$HOME/.cache}/hls-temp"
jobdir="${XDG_CACHE_HOME:-$HOME/.cache}/hls-jobs" jobdir="${XDG_CACHE_HOME:-$HOME/.cache}/hls-jobs"
while getopts 'fs' OPT; do while getopts 'o:rfhn:' OPT; do
case $OPT in case $OPT in
f) file=$OPTARG ;; o) file=$OPTARG ;;
s) skip_res=1;; n) n=$OPTARG ;;
f) skip_ffmpeg=1;;
r) skip_res=1;;
*|h) *|h)
help_text help_text
exit 0 exit 0
@@ -38,18 +43,20 @@ shift $((OPTIND - 1))
trap "rm -rdf $tmpdir $jobdir;exit 0" INT HUP trap "rm -rdf $tmpdir $jobdir;exit 0" INT HUP
printf "\033[2K\r\033[1;36mFetching resolutions.." printf "\033[2K\r\033[1;36mFetching resolutions.."
m3u8_data=$(curl -s "$link") m3u8_data=$(curl -s "$link")
res_list=$(printf "%s" "$m3u8_data" | sed -nE 's_.*RESOLUTION=.*x([^,]*),.*_\1_p') res_list=$(printf "%s" "$m3u8_data" | sed -nE 's_.*RESOLUTION=.*x([^,]*).*_\1_p')
if [ -n "$res_list" ];then if [ -n "$res_list" ];then
highest_res=$(printf "$res_list" | sort -nr | head -1) highest_res=$(printf "$res_list" | sort -nr | head -1)
[ "$skip_res" -eq 1 ] && printf "\033[2K\r\033[1;36mSelecting highest resolution.." || (printf "\033[2K\r\033[1;33mRESOLUTIONS >>\n\033[0m$res_list\n\033[1;34mType ur preferred resolution (default: $highest_res) > " && read -r sel_res) [ "$skip_res" -eq 1 ] && printf "\033[2K\r\033[1;36mSelecting highest resolution.." || (printf "\033[2K\r\033[1;33mRESOLUTIONS >>\n\033[0m$res_list\n\033[1;34mType ur preferred resolution (default: $highest_res) > " && read -r sel_res)
[ -z "$sel_res" ] && sel_res=$highest_res [ -z "$sel_res" ] && sel_res=$highest_res
unset highest_res res_list unset highest_res res_list
url=$(printf "%s" "$m3u8_data" | sed -n "/$sel_res,/{n;p;}" | tr -d '\r') url=$(printf "%s" "$m3u8_data" | sed -n "/x$sel_res/{n;p;}" | tr -d '\r')
#check whether the m3u8_data contains uri that starts from http #check whether the m3u8_data contains uri that starts from http
printf "%s" "$m3u8_data" | grep -q "http" || relative_url=$(printf "%s" "$link" | sed 's_[^/]*$__') printf "%s" "$m3u8_data" | grep -q "http" || relative_url=$(printf "%s" "$link" | sed 's_[^/]*$__')
printf "\033[2K\r\033[1;36mFetching Metadata.." printf "\033[2K\r\033[1;36mFetching Metadata.."
resp="$(curl -s "${relative_url}$url")" url="${relative_url}$url"
resp="$(curl -s "$url")"
else else
url=$link
resp=$m3u8_data resp=$m3u8_data
fi fi
[ -d "$tmpdir" ] || mkdir -p "$tmpdir" [ -d "$tmpdir" ] || mkdir -p "$tmpdir"
@@ -57,7 +64,11 @@ fi
key_uri="$(printf "%s" "$resp" | sed -nE 's/^#EXT-X-KEY.*URI="([^"]*)"/\1/p')" 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')" [ -z "$key_uri" ] || iv_uri="$(printf "%s" "$resp" | sed -nE 's/^#EXT-X-IV.*URI="([^"]*)"/\1/p')"
data="$(printf "%s" "$resp" | sed '/#/d')" data="$(printf "%s" "$resp" | sed '/#/d')"
printf "%s" "$data" | grep -q "http" && relative_url='' || relative_url=$(printf "%s" "$link" | sed 's_[^/]*$__') if printf "%s" "$data" | grep -q "http";then
relative_url=''
else
relative_url=$(printf "%s" "$url" | sed 's_[^/]*$__')
fi
range=$(printf "%s\n" "$data" | wc -l) range=$(printf "%s\n" "$data" | wc -l)
#for encrypted stream only #for encrypted stream only
@@ -73,7 +84,7 @@ printf "\033[2K\r\033[1;35mpieces : $range\n\033[1;33mDownloading.."
for i in $(seq $range); do 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 "%04d" "$i").ts" && printf "\033[2K\r\033[1;32m ✓ $i / $range done" &
jobs -p > "$jobdir" jobs -p > "$jobdir"
while [ "$(cat "$jobdir" | wc -w)" -ge 36 ];do jobs > "$jobdir";sleep 0.05;done while [ "$(cat "$jobdir" | wc -l)" -ge $n ];do jobs > "$jobdir";sleep 0.05;done
done done
wait wait
@@ -92,8 +103,8 @@ fi
rm -rdf $tmpdir $jobdir rm -rdf $tmpdir $jobdir
#conversion of allts file to mp4 video using ffmpeg.. #conversion of allts file to mp4 video using ffmpeg..
printf "\033[2K\r\033[1;36mEncoding file to mp4 video..\n\033[0m" 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" [ -z "$skip_ffmpeg" ] && ffmpeg -i "$file.ts" -loglevel error -stats -c copy "$file.mp4" || mv "$file.ts" "$file.mp4"
#cleanup.. #cleanup..
rm $file.ts rm "$file".ts
printf "\033[2K\r\033[1;36m Done!!" printf "\033[2K\r\033[1;36m Done!!"