Skip to content

Instantly share code, notes, and snippets.

@makoto1984
Last active January 15, 2021 15:57
Show Gist options
  • Save makoto1984/49ac5e19b460b61ae898a27df9012470 to your computer and use it in GitHub Desktop.
Save makoto1984/49ac5e19b460b61ae898a27df9012470 to your computer and use it in GitHub Desktop.
streaming music with generated video to youtube via ffmpeg
#!/bin/sh
# This script is a companion of the script youtube-music-live.sh. Assuming the MUSIC_PATH is an icecast2 stream, The script will fetch the metadata tag StreamTitle every 5 seconds and update the file /tmp/artist, /tmp/title. Generally, the StreamTitle should be like "artist - title", though sometimes there is only title. You may also update the files manually and ignore this script. Be sure to run this script after youtube-music-live.sh.
# You have to define music url.
MUSIC_PATH=
while [ `pgrep -x ffmpeg` ]
do
meta=`ffprobe -v error -show_format "$MUSIC_PATH" | grep StreamTitle | cut -d= -f2`
case "$meta" in
*\ -\ *) artist=`echo $meta|cut -d- -f1`;title=`echo $meta|sed "s/$artist-\ //"`;echo $title>/tmp/title;echo $artist>/tmp/artist;;
*) echo $meta>/tmp/title;echo >/tmp/artist;;
esac
sleep 5
done
#!/bin/sh
# This script streams music to youtube with generated video. Resolution of 1920x1080 is assumed. The video has a background image 1920x1080, overlaid by spectrum in the center, music title and artist at top, a logo at bottom-right and scrolling subtitles at the bottom.
# You have to define youtube streaming key, background image filename, logo filename, music filename or url.
KEY=
YOUTUBE_BG=
YOUTUBE_LOGO=
MUSIC_PATH=
# Any changes in these files will update the text in the video immediately.
echo artist > /tmp/artist
echo title > /tmp/title
echo 'Please subscribe if you like it.' > /tmp/subtitle
ffmpeg -thread_queue_size 1024 -i "$MUSIC_PATH" -i "$YOUTUBE_BG" -i "$YOUTBE_LOGO" -filter_complex "[0:a]showcqt=s=1920x716:axis=0:bar_h=390:bar_v=50:sono_h=326,pad=w=1920:h=1080:y=364[sc];[1:v]setsar=1/1[bg];[sc][bg]blend=all_mode=grainmerge,drawbox=x=277:y=64:w=1366:h=300:color=black@0.5:t=fill,drawtext=fontcolor=white:fontsize=88:x=448:y=120:textfile=title:reload=1,drawtext=fontcolor=white:fontsize=72:x=448:y=256:textfile=artist:reload=1,drawtext=box=1:boxcolor=black@0.5:fontcolor=white:fontsize=60:x=w-mod(t*(w+tw)/120\,(w+tw)):y=1000:textfile=subtitle:reload=1[mix];[mix][2:v]overlay=x=W-w-50:y=H-h-100[out]" -map "[out]" -map 0:a -c:v libx264 -crf 28 -preset:v veryfast -profile:v high -tune:v zerolatency -c:a libfdk_aac -vbr 5 -f flv rtmp://a.rtmp.youtube.com/live2/$KEY
# Use the command below if you want to utilize vaapi acceleration
#ffmpeg -thread_queue_size 1024 -i "$MUSIC_PATH" -i "$YOUTUBE_BG" -i "$YOUTUBE_LOGO" -vaapi_device /dev/dri/renderD128 -filter_complex "[0:a]showcqt=s=1920x716:axis=0:bar_h=390:bar_v=50:sono_h=326,pad=w=1920:h=1080:y=364[sc];[1:v]setsar=1/1[bg];[sc][bg]blend=all_mode=grainmerge,drawbox=x=277:y=64:w=1366:h=300:color=black@0.5:t=max,drawtext=fontcolor=white:fontsize=88:x=448:y=120:textfile=/tmp/title:reload=1,drawtext=fontcolor=white:fontsize=72:x=448:y=256:textfile=/tmp/artist:reload=1,drawtext=box=1:boxcolor=black@0.5:fontcolor=white:fontsize=60:x=w-mod(t*(w+tw)/120\,(w+tw)):y=1000:textfile=/tmp/subtitle:reload=1[mix];[mix][2:v]overlay=x=W-w-50:y=H-h-100,format=nv12,hwupload[out]" -map "[out]" -map 0:a -c:v h264_vaapi -c:a libfdk_aac -vbr 5 -f flv rtmp://a.rtmp.youtube.com/live2/$KEY
# Use the command below if you want to utilize nvenc acceleration. You may need to add primusrun before ffmpeg if you have bumblebee.
#ffmpeg -thread_queue_size 1024 -i "$MUSIC_PATH" -i "$YOUTUBE_BG" -i "$YOUTUBE_LOGO" -filter_complex "[0:a]showcqt=s=1920x716:axis=0:bar_h=390:bar_v=50:sono_h=326,pad=w=1920:h=1080:y=364[sc];[1:v]setsar=1/1[bg];[sc][bg]blend=all_mode=grainmerge,drawbox=x=277:y=64:w=1366:h=300:color=black@0.5:t=max,drawtext=fontcolor=white:fontsize=88:x=448:y=120:textfile=/tmp/title:reload=1,drawtext=fontcolor=white:fontsize=72:x=448:y=256:textfile=/tmp/artist:reload=1,drawtext=box=1:boxcolor=black@0.5:fontcolor=white:fontsize=60:x=w-mod(t*(w+tw)/120\,(w+tw)):y=1000:textfile=/tmp/subtitle:reload=1[mix];[mix][2:v]overlay=x=W-w-50:y=H-h-100[out]" -map "[out]" -map 0:a -c:v h264_nvenc -preset:v llhq -profile:v high -cq 28 -c:a libfdk_aac -vbr 5 -f flv rtmp://a.rtmp.youtube.com/live2/$KEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment