Skip to content

Instantly share code, notes, and snippets.

@SMarioMan
Created October 16, 2024 00:00
Show Gist options
  • Select an option

  • Save SMarioMan/f6b6348e3294c1644d59703f7e9e8014 to your computer and use it in GitHub Desktop.

Select an option

Save SMarioMan/f6b6348e3294c1644d59703f7e9e8014 to your computer and use it in GitHub Desktop.
Audio to Video Conversion for YouTube
#!/bin/bash
# Define the folder containing the tracks
# Defaults to the "Tracks" folder
TRACKS_FOLDER="${1:-Tracks}"
# Iterate through each file in the folder
for file in "$TRACKS_FOLDER"/*
do
# Check if it's a file (not a directory) and (hopefully) a media file
if [ -f "$file" ] &&
[[ "${file##*.}" != "mp4" ]] &&
[[ "${file##*.}" != "ini" ]] &&
[[ "${file##*.}" != "DS_Store" ]]; then
# Call the script with the current file
bash music-to-video.sh "$file"
fi
done
#!/bin/bash
# Extract title and subtitle from filename
extract_title_subtitle() {
local filename="$1"
# Extract title (text before the first dash)
local title=$(echo "$filename" | awk -F ' - ' '{print $1}')
# Extract subtitle (text between the first and second dash, if they exist)
local subtitle=$(echo "$filename" | awk -F ' - ' '{if (NF >= 3) print $2}')
# Trim whitespace
title=$(echo "$title" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
subtitle=$(echo "$subtitle" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo -n "$title" > title.txt
echo -n "$subtitle" > subtitle.txt
}
# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <music_file_path>"
exit 1
fi
# Assign command line arguments to variables
MUSIC_FILE="$1"
COVER_IMAGE="cover.png"
OUTPUT_FILE="${MUSIC_FILE%.*}.mp4"
# Target image width and height, in pixels
ART_RES=2160
# Get title and subtitle information
$(extract_title_subtitle "$(basename "$MUSIC_FILE" .flac)")
# Get the duration of the audio file using ffprobe
DURATION=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$MUSIC_FILE")
# Check if the duration was successfully retrieved
if [ -z "$DURATION" ]; then
echo "Error: Unable to retrieve audio duration from $MUSIC_FILE"
exit 1
fi
# Calculate the fade-out start time (3 seconds before the end)
FADE_OUT_START=$(echo "$DURATION - 3" | bc)
# Create a temporary image file with the desired text overlay
# Assumes the desired font, Comic Sans, is in the current directory as COMIC.TTF
# Font size and poisition will depend on the cover size; here it is 4096x4096
TEMP_IMAGE="temp_image.png"
echo \"$TITLE\"
echo \"$SUBTITLE\"
ffmpeg -i "$COVER_IMAGE" \
-vf "drawtext=textfile=title.txt:fontfile=COMIC.TTF:fontsize=240:fontcolor=white:x=320:y=150:box=1:boxcolor=black@0.5:boxborderw=20:line_spacing=-20,
drawtext=textfile=subtitle.txt:fontfile=COMIC.TTF:fontsize=120:fontcolor=white:x=320:y=500:box=1:boxcolor=black@0.5:boxborderw=10:line_spacing=-10" \
"$TEMP_IMAGE"
# Run the ffmpeg command to generate the video
# Apply a 3-second fade on either side
# Preserve the original FLAC audio; nonstandard but preserves quality
ffmpeg -loop 1 -i "$TEMP_IMAGE" -i "$MUSIC_FILE" \
-vf "fade=t=in:st=0:d=3,fade=t=out:st=$FADE_OUT_START:d=3,scale=$ART_RES:$ART_RES" \
-c:v libx264 -tune stillimage -pix_fmt yuv420p -r 60 \
-c:a flac -strict -2 \
-shortest "$OUTPUT_FILE"
# Remove temporary data
rm "$TEMP_IMAGE" title.txt subtitle.txt
# Check if the command succeeded
if [ $? -eq 0 ]; then
echo "Video created successfully: $OUTPUT_FILE"
else
echo "Error creating video."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment