Shell script to convert MP4 or WEBM video files to MP3 with thumbnails

Hello, this is Munou.
When converting video directly to an audio file with ffmpeg, the thumbnail is often lost, so I tried writing a shell script.
It just converts, then extracts and embeds the thumbnail, then deletes unnecessary files and restores the original name.
#!/bin/bash
# Respective directory paths
input_dir=“/media/m”
output_dir=“/media/m/mp3”
# Create output directory if it doesn't exist
mkdir -p “$output_dir”
# Format specification *Currently only converts to mp3, but just in case I feel motivated to add branching for other formats later...
in=“mp4”
out=“mp3”
# Apply to all files in the directory
for file in “$input_dir”/*.${in}; do
# Specify base name
base=$(basename “$file” .${in})
# Specify MP3 path
output=“${output_dir}/${base}.${out}”
# Specify thumbnail path
thumbnail=“${output_dir}/${base}_thumbnail.jpg”
# Convert video file to mp3
ffmpeg -i “$file” -vn -acodec libmp3lame -qscale:a 2 “$output”
# Extract thumbnail from video file
if ffmpeg -i “$file” -an -vframes 1 -q:v 2 “$thumbnail” -y; then
echo “Thumbnail extracted for $base”
ffmpeg -i “$output” -i “$thumbnail” -map 0 -map 1 -c copy -id3v2_version 3 -metadata:s:v title=“Alb
um cover” -metadata:s:v comment=“Cover (front)” -y “${output_dir}/${base}_with_thumbnail.${out}”
rm “${output_dir}/${base}.${out}”
rm “${output_dir}/${base}_thumbnail.jpg”
mv “${output_dir}/${base}_with_thumbnail.${out}” “${output_dir}/${base}.${out}”
else
echo “No found for $base”
fi
done
Grant execution permission
chmod +x convert.sh
Since I was connected via SSH, I'll make it run in the background even if the session is disconnected.
nohup ./convert.sh &
That's all.