Skip to content

Instantly share code, notes, and snippets.

@infojunkie
Last active August 24, 2021 04:26
Show Gist options
  • Save infojunkie/6f9e6d0c9dce9be44116b7a828accc20 to your computer and use it in GitHub Desktop.
Save infojunkie/6f9e6d0c9dce9be44116b7a828accc20 to your computer and use it in GitHub Desktop.
Convert audio track/album to YouTube video

youtube-album

A recipe to prepare audio file(s) for YouTube upload. Video will be a slideshow of images on repeat.

Requires: imagemagick ffmpeg

  1. Convert images to required size. Here I assume .png files and output to png/ folder. See http://www.imagemagick.org/Usage/crop/#extent
for f in *.png; do convert "$f" -resize 800x600 -gravity center -background black -extent 800x600 png/"$f".png; done
  1. Generate video out of pngs. Here each image is shown for 20 seconds. See https://trac.ffmpeg.org/wiki/Slideshow and https://en.wikibooks.org/wiki/FFMPEG_An_Intermediate_Guide/image_sequence#Filename_patterns
ffmpeg -framerate 1/20 -pattern_type glob -i "png/*.png" -c:v libx264 -vf "fps=25,format=yuv420p" png.mp4
  1. Concatenate audio files. See https://trac.ffmpeg.org/wiki/Concatenate
printf "file '%s'\n" ./*.flac > files.txt
ffmpeg -f concat -safe 0 -i files.txt files.flac
  1. Concatenate png video enough times to cover audio duration. See http://superuser.com/a/1116107/55867 for rationale.
for i in {1..10}; do printf "file '%s'\n" png.mp4 >> pngs.txt; done
ffmpeg -f concat -safe 0 -i pngs.txt -c copy pngs.mp4
  1. Create YouTube video using acceptable encoding settings. See https://www.virag.si/2015/06/encoding-videos-for-youtube-with-ffmpeg/
ffmpeg -i pngs.mp4 -i files.flac -shortest -codec:v libx264 -crf 21 -bf 2 -flags +cgop -pix_fmt yuv420p -codec:a aac -strict -2 -b:a 384k -r:a 48000 -movflags faststart youtube.mp4
  1. Generate a YouTube track listing to copy-paste in the video description.
youtube-playlist.sh *.flac
  1. Generate a video file with chapters.
youtube-chapters.sh *.flac > chapters
ffmpeg -i youtube.mp4 -i chapters -map_metadata 1 -codec copy youtube-chapters.mp4
#!/bin/bash
# Show album track list in ffmpeg metadata format
#
# Only works with .flac atm :-(
#
# Requires: metaflac sox
# https://stackoverflow.com/a/43444305/209184
round() {
printf "%.${2}f" "${1}"
}
echo ";FFMETADATA1"
offset=0
for f in "$@"
do
start=$(round $offset 0)
echo "[CHAPTER]"
echo "TIMEBASE=1/1000"
echo "START=$start"
delta=$(soxi -D "$f")
offset=$(bc -l <<< "$offset + ($delta * 1000)")
end=$(round $offset 0)
echo "END=$end"
title=$(metaflac "$f" --show-tag=TITLE | sed s/.*=//g)
echo "TITLE=$title"
done
#!/bin/bash
# Show album track list in YouTube-friendly format including
# clickable track offsets.
#
# Only works with .flac atm :-(
#
# Requires: metaflac sox
start=0
for f in "$@"
do
offset=$(date -u -d "0 $start sec" +"%H:%M:%S")
title=$(metaflac "$f" --show-tag=TITLE | sed s/.*=//g)
trackno=$(metaflac "$f" --show-tag=TRACKNUMBER | sed s/.*=//g)
echo "$offset" "$trackno" "$title"
end=$(soxi -D "$f")
start=$(bc -l <<< "$start + $end")
done
@infojunkie
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment