From 83552731d8c649d25633052f9078e36e96b74d34 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Thu, 26 Jan 2023 20:38:00 +0400 Subject: [PATCH] Changed filename template, converting script, some improvements --- .convert_helper.sh | 2 ++ autoytdlp.sh | 53 +++++++++++++++++++++++++++++++++++++++------- convert.sh | 18 ++++++++++++++++ 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 .convert_helper.sh create mode 100755 convert.sh diff --git a/.convert_helper.sh b/.convert_helper.sh new file mode 100644 index 0000000..d8bcc7b --- /dev/null +++ b/.convert_helper.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +ffmpeg -i "$1" "$(echo "$1" | sed -E "s/\.[A-Za-z0-9]+$/.$2/")" diff --git a/autoytdlp.sh b/autoytdlp.sh index 5c25e9a..f12636f 100755 --- a/autoytdlp.sh +++ b/autoytdlp.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash +debug=0 watching=1 links=() @@ -26,6 +27,11 @@ ytlink () { sed -E 's#^https?://([A-Za-z0-9.-]+/watch\?v=|(yt\.be|youtu\.be)/)([A-Za-z0-9_-]+)|.+#\3#' } +safename () { + safe="${1//[^A-Za-z0-9А-Яа-я ().,_-]/}" + echo "${safe// /_}" | sed -E 's/_+/_/' +} + bold () { echo -e "\033[1m$1\033[0m" } @@ -67,13 +73,20 @@ dlwith_piped () { echo "Parsing link" video_id=$(ytlink "$link") + if [[ $video_id == "" ]]; then + echo 'Unable to parse YT video ID, skipping' + continue + fi + echo "Found YT video ID: $video_id" + echo "Requesting URL for $link" - video_obj=$(curl -sL "https://ytapi.dc09.ru/streams/$video_id" | jq "$jqexpr") + video_obj=$(curl -sL "https://ytapi.dc09.ru/streams/$video_id") - video_url=$(echo "$video_obj" | jq ".url" | sed s/^\"// | sed s/\"$//) + stream_obj=$(echo "$video_obj" | jq "$jqexpr") + stream_url=$(echo "$stream_obj" | jq ".url" | sed s/^\"// | sed s/\"$//) - video_mime=$(echo "$video_obj" | jq ".mimeType") - case "$video_mime" in + stream_mime=$(echo "$stream_obj" | jq ".mimeType") + case "$stream_mime" in "\"audio/mp4\"") ext='m4a' ;; @@ -91,10 +104,16 @@ dlwith_piped () { ;; esac - video_file="./files/${video_id}.${ext}" + video_title=$(echo "$video_obj" | jq ".title") + video_file="./files/$(safename "$video_title").${ext}" + echo "Filename: $video_file" + + if [[ $debug == 1 ]] + then continue + fi echo "Downloading with wget" - wget -O "$video_file" "$video_url" + wget -O "$video_file" "$stream_url" echo done @@ -109,6 +128,12 @@ dlwith_ytdlp () { echo 'details: https://github.com/yt-dlp/yt-dlp#format-selection' read -r format + if [[ $format == "" ]]; then + echo 'Passed an empty string,' + echo 'using "b" as default.' + format="b" + fi + echo bold 'Started' echo @@ -118,14 +143,26 @@ dlwith_ytdlp () { video_id=$(ytlink "$link") if [[ $video_id != "" ]]; then # Convert YT and Piped links to YT + echo "Found YT video ID: $video_id" newlink="https://youtube.com/watch?v=$video_id" else newlink="$link" fi - echo "URL: $newlink" - yt-dlp -f "$format" -o "%(id)s.%(ext)s" -P ./files/ "$newlink" + echo "Generating safe name for the file" + video_title=$(yt-dlp --print title "$newlink") + video_file="$(safename "$video_title")---%(id)s.%(ext)s" + echo "Template: $video_file" + + if [[ $debug == 1 ]] + then continue + fi + + echo "Downloading with yt-dlp" + yt-dlp -f "$format" -o "$video_file" -P ./files/ "$newlink" + + echo done } diff --git a/convert.sh b/convert.sh new file mode 100755 index 0000000..ec75b17 --- /dev/null +++ b/convert.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +trap 'echo; echo "Exit"; exit 0' SIGINT + +echo "Enter input files extension:" +echo "(empty string to match all)" +read -r ext_input + +if [[ $ext_input == "" ]]; then + ext_input="*" +fi + +echo "Enter output files extension:" +echo "(must not be empty)" +read -r ext_output + +find ./files -type f -name "*.$ext_input" -exec \ +bash ./.convert_helper.sh {} "$ext_output" \;