update hls code

This commit is contained in:
Tanveer Ahmed Ansari
2024-02-01 14:03:51 +05:30
committed by GitHub
parent 2ca49998fe
commit ec8d6fa49d

35
hls
View File

@@ -21,11 +21,11 @@ help_text () {
} }
download(){ download(){
printf "" > $failed printf "" > "$failed"
for i in $1; do 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" 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 done
wait wait
} }
@@ -43,7 +43,7 @@ while getopts 'o:rfhn:s:' OPT; do
f) skip_ffmpeg=1;; f) skip_ffmpeg=1;;
r) skip_res=1;; r) skip_res=1;;
s) subs=$OPTARG;; s) subs=$OPTARG;;
*|h) *)
help_text help_text
exit 0 exit 0
;; ;;
@@ -52,18 +52,18 @@ done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
[ -z "$*" ] && printf "\033[1;34mEnter link >\033[0m " && read -r link || link=$* [ -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.." 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 "%s" "$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.." [ -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 [ -z "$sel_res" ] && sel_res=$highest_res
unset highest_res res_list unset highest_res res_list
url=$(printf "%s" "$m3u8_data" | sed -n "/x$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" "$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.." printf "\033[2K\r\033[1;36mFetching Metadata.."
url="${relative_url}$url" url="${relative_url}$url"
resp="$(curl -s "$url")" resp="$(curl -s "$url")"
@@ -76,7 +76,7 @@ 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" "$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) range=$(printf "%s\n" "$data" | wc -l)
#for encrypted stream only #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') [ -z "$iv_uri" ] && iv=$(openssl rand -hex 16) || iv=$(curl -s "$iv_uri" | od -A n -t x1 | tr -d ' |\n')
fi 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 #downloading .ts data asynchronously
if command -v aria2c >>/dev/null;then 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 -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 -x16 -s16 -j "$n" -k'1M' -d "$tmpdir" -i - --download-result=hide --summary-interval=0 --allow-overwrite=true
else else
download "$(seq $range)" download "$(seq "$range")"
#redownloading failed pieces #redownloading failed pieces
download "$(cat $failed)" download "$(cat "$failed")"
fi fi
#downloading subtitles if uri passed using -s option #downloading subtitles if uri passed using -s option
[ -z "$subs" ] || curl -s "$subs" -o "$file.srt" & [ -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" openssl aes-128-cbc -d -K "$key" -iv "$iv" -nopad >> "$file.ts" < "$i"
done done
else else
printf "\033[2K\r\033[1;36m Concatenating pieces into single file.." printf "\033[2K\r\033[1;36m Concatenating pieces into single file..\n"
cat "$tmpdir"/* >> "$file.ts" cat "$tmpdir"/* | ffmpeg -loglevel error -stats -i - -c copy "$file.mp4" -y
skip_ffmpeg=1
fi fi
rm -rdf $tmpdir $jobdir $failed rm -rdf "$tmpdir" "$jobdir" "$failed"
#conversion of allts file to mp4 video using ffmpeg.. #conversion of allts file to mp4 video using ffmpeg..
if [ -z "$skip_ffmpeg" ];then if [ -z "$skip_ffmpeg" ];then
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" ffmpeg -i "$file.ts" -loglevel error -stats -c copy "$file.mp4"
else else
mv "$file.ts" "$file.mp4" [ -f "$file.ts" ] && mv "$file.ts" "$file.mp4"
fi fi
#cleanup.. #cleanup..