mirror of
https://github.com/CoolnsX/hls_downloader.git
synced 2025-12-20 07:15:21 +05:30
Merge branch 'main' into patch-1
This commit is contained in:
37
hls
37
hls
@@ -21,11 +21,11 @@ help_text () {
|
||||
}
|
||||
|
||||
download(){
|
||||
printf "" > $failed
|
||||
printf "" > "$failed"
|
||||
for i in $1; do
|
||||
curl --max-time 30 -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" || printf "$i\n" >> $failed &
|
||||
curl --max-time 30 -s "${relative_url}$(printf "%s" "$data" | sed -n "${i}p")" > "$tmpdir/$(printf "%05d" "$i").ts" && printf "\033[2K\r\033[1;32m ✓ %s / %s done" "$i" "$range" || printf "%s\n" "$i" >> "$failed" &
|
||||
jobs -p > "$jobdir"
|
||||
while [ "$(cat "$jobdir" | wc -l)" -ge $n ];do jobs > "$jobdir";sleep 0.05;done
|
||||
while [ "$(wc -l "$jobdir")" -ge "$n" ];do jobs > "$jobdir";sleep 0.05;done
|
||||
done
|
||||
wait
|
||||
}
|
||||
@@ -43,7 +43,7 @@ while getopts 'o:rfhn:s:' OPT; do
|
||||
f) skip_ffmpeg=1;;
|
||||
r) skip_res=1;;
|
||||
s) subs=$OPTARG;;
|
||||
*|h)
|
||||
*)
|
||||
help_text
|
||||
exit 0
|
||||
;;
|
||||
@@ -52,18 +52,18 @@ done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
[ -z "$*" ] && printf "\033[1;34mEnter link >\033[0m " && read -r link || link=$*
|
||||
trap "killall curl;rm -rdf $tmpdir $jobdir;exit 0" INT HUP
|
||||
trap "killall curl;rm -rdf '$tmpdir' '$jobdir';exit 0" INT HUP
|
||||
printf "\033[2K\r\033[1;36mFetching resolutions.."
|
||||
m3u8_data=$(curl -s "$link")
|
||||
m3u8_data=$(curl -s "$link" | sed '/#EXT-X-I-FRAME-STREAM-INF/d')
|
||||
res_list=$(printf "%s" "$m3u8_data" | sed -nE 's_.*RESOLUTION=.*x([^,]*).*_\1_p')
|
||||
if [ -n "$res_list" ];then
|
||||
highest_res=$(printf "$res_list" | sort -nr | head -1)
|
||||
[ -z "$skip_res" ] && 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 || printf "\033[2K\r\033[1;36mSelecting highest resolution.."
|
||||
highest_res=$(printf "%s" "$res_list" | sort -nr | head -1)
|
||||
[ -z "$skip_res" ] && printf "\033[2K\r\033[1;33mRESOLUTIONS >>\n\033[0m%s\n\033[1;34mType ur preferred resolution (default: %s) > " "$res_list" "$highest_res" && read -r sel_res || printf "\033[2K\r\033[1;36mSelecting highest resolution.."
|
||||
[ -z "$sel_res" ] && sel_res=$highest_res
|
||||
unset highest_res res_list
|
||||
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
|
||||
printf "%s" "$url" | grep -q "http" || relative_url=$(printf "%s" "$link" | sed 's_[^/]*$__')
|
||||
printf "%s" "$url" | grep -q "http" || relative_url=$(printf "%s" "$link" | sed 's|[^/]*$||')
|
||||
printf "\033[2K\r\033[1;36mFetching Metadata.."
|
||||
url="${relative_url}$url"
|
||||
resp="$(curl -s "$url")"
|
||||
@@ -76,7 +76,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')"
|
||||
printf "%s" "$data" | grep -q "http" && relative_url='' || relative_url=$(printf "%s" "$url" | sed 's_[^/]*$__')
|
||||
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
|
||||
@@ -87,14 +87,14 @@ if [ -n "$key_uri" ];then
|
||||
[ -z "$iv_uri" ] && iv=$(openssl rand -hex 16) || iv=$(curl -s "$iv_uri" | od -A n -t x1 | tr -d ' |\n')
|
||||
fi
|
||||
|
||||
printf "\033[2K\r\033[1;35mpieces : $range\n\033[1;33mDownloading.."
|
||||
printf "\033[2K\r\033[1;35mpieces : %s\n\033[1;33mDownloading.." "$range"
|
||||
#downloading .ts data asynchronously
|
||||
if command -v aria2c >>/dev/null;then
|
||||
printf '%s' "$data" | nl -n'rz' | sed -E "s|^([0-9]*)[[:space:]]*(.*)|${relative_url}\2\n\tout=\1.ts|g" | aria2c --no-conf=true --enable-rpc=false -x16 -s16 -j "$n" -d "$tmpdir" -i - --download-result=hide --summary-interval=0
|
||||
printf '%s' "$data" | nl -n'rz' | sed -E "s|^([0-9]*)[[:space:]]*(.*)|${relative_url}\2\n\tout=\1.ts|g" | aria2c --no-conf=true --enable-rpc=false -x16 -s16 -j "$n" -k'1M' -d "$tmpdir" -i - --download-result=hide --summary-interval=0
|
||||
else
|
||||
download "$(seq $range)"
|
||||
download "$(seq "$range")"
|
||||
#redownloading failed pieces
|
||||
download "$(cat $failed)"
|
||||
download "$(cat "$failed")"
|
||||
fi
|
||||
#downloading subtitles if uri passed using -s option
|
||||
[ -z "$subs" ] || curl -s "$subs" -o "$file.srt" &
|
||||
@@ -107,17 +107,18 @@ if [ -n "$key_uri" ];then
|
||||
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.."
|
||||
cat "$tmpdir"/* >> "$file.ts"
|
||||
printf "\033[2K\r\033[1;36m Concatenating pieces into single file..\n"
|
||||
cat "$tmpdir"/* | ffmpeg -loglevel error -stats -i - -c copy "$file.mp4" -y
|
||||
skip_ffmpeg=1
|
||||
fi
|
||||
|
||||
rm -rdf $tmpdir $jobdir $failed
|
||||
rm -rdf "$tmpdir" "$jobdir" "$failed"
|
||||
#conversion of allts file to mp4 video using ffmpeg..
|
||||
if [ -z "$skip_ffmpeg" ];then
|
||||
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"
|
||||
else
|
||||
mv "$file.ts" "$file.mp4"
|
||||
[ -f "$file.ts" ] && mv "$file.ts" "$file.mp4"
|
||||
fi
|
||||
|
||||
#cleanup..
|
||||
|
||||
Reference in New Issue
Block a user