#!/bin/bash # function to convert to snake_case to_snake_case() { echo "$1" | tr '[:upper:]' '[:lower:]' | \ sed -e 's/[^a-zA-Z0-9]/_/g' -e 's/__*/_/g' -e 's/^_//' -e 's/_$//' } # function to sanitize text for ImageMagick sanitize() { echo "$1" | sed 's/"/\\"/g' } # function to truncate text truncate_text() { local text="$1" if [ ${#text} -gt 50 ]; then echo "${text:0:47}..." else echo "$text" fi } # function to process title - remove everything before the - if it exists process_title() { local title="$1" if [[ "$title" == *-* ]]; then title=$(echo "$title" | sed 's/^.*- *//') fi echo "$title" } # get the title to check if file exists echo "Checking metadata..." TITLE=$(yt-dlp --get-title "$1") TITLE=$(process_title "$TITLE") TITLE_TRUNCATED=$(truncate_text "$TITLE") FILENAME=$(to_snake_case "$TITLE_TRUNCATED") # check if file exists if [ -f "${FILENAME}.mp4" ]; then echo "Error: ${FILENAME}.mp4 already exists" exit 1 fi # download audio echo "Downloading audio..." yt-dlp -f bestaudio --extract-audio --audio-format mp3 --output "temp_audio.%(ext)s" --print-json "$1" > metadata.json 2>/dev/null ARTIST=$(cat metadata.json | jq -r '.uploader') YEAR=$(cat metadata.json | jq -r '.upload_date[0:4]') DURATION=$(cat metadata.json | jq -r '.duration') rm metadata.json # format duration to MM:SS DURATION_FORMATTED=$(printf "%02d:%02d" $((DURATION/60)) $((DURATION%60))) # download thumbnail echo "Downloading thumbnail..." yt-dlp --write-thumbnail --skip-download --convert-thumbnails jpg "$1" -o "temp_thumb" 2>/dev/null # create title card using the external script echo "Generating title card..." ./create_title_card.sh temp_thumb.jpg "$(sanitize "$TITLE_TRUNCATED")" "$(sanitize "$ARTIST")" "$YEAR" "$DURATION_FORMATTED" # create MP4 with title card and audio echo "Creating final video..." ffmpeg -loop 1 -i output_title_card.png -i temp_audio.mp3 \ -c:v libx264 -preset ultrafast -tune stillimage \ -c:a aac -b:a 192k -pix_fmt yuv420p \ -shortest \ -movflags +faststart \ "${FILENAME}.mp4" 2>/dev/null # clean up temporary files rm temp_thumb.jpg output_title_card.png temp_audio.mp3 echo "Done! Created ${FILENAME}.mp4"