Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Last active August 4, 2018 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmerrick/3d128c32bb9d6b8a6339d398c3a288ba to your computer and use it in GitHub Desktop.
Save dmerrick/3d128c32bb9d6b8a6339d398c3a288ba to your computer and use it in GitHub Desktop.

A Dana Life... Live on Twitch!

Welcome!

This is the source code to twitch.tv/ADanaLife_

It is composed of two parts:

  1. A Ruby script that generates a randomized playlist
  2. A Bash script that starts an ffmpeg process

Thanks for watching!

-Dana

dana.lol

#!/usr/bin/env ruby
# this ugly script generates a playlist for FFMPEG
# use the dir passed in via CLI
vid_dir = ARGV.shift
# find all the files in the dir
all_files = Dir.glob("#{vid_dir}/**/*")
# but only incude MP4 files
files = all_files.select{|f| f =~ /\.MP4$/ }
# create a hash that organizes files by date taken
by_day = {}
files.each do |file|
date = file.sub(/.*\//,'')[0..8]
by_day[date] ||= []
by_day[date] << file
end
# since some files are in a "read-only" subdir, make sure
# those are in the correct order when sorted
by_day.each do |date, file_list|
file_list.sort! do |a,b|
a.sub(/\/RO/,'') <=> b.sub(/\/RO/,'')
end
end
# print the formatted output for ffmpeg
by_day.keys.shuffle.each do |day|
by_day[day].each do |file|
puts "file '#{file}'"
end
end
#!/usr/bin/env bash
# This script takes a directory of videos and streams them to Twitch
#
# I use it for streaming dashcam footage, and because of this I apply
# a filter to blur out some a portion of the screen.
#TODO: check if we have ffmpeg and fail gracefully
#TODO: the blur needs to not cover the E/W
if [[ $# -eq 0 ]] ; then
echo "Usage: $0 [stream key] [dir containing vids]"
exit 1
fi
STREAM_KEY="$1"
VID_DIR="$2"
ffmpeg \
-hide_banner \
-f concat \
-safe 0 \
-i <(./smart-shuffle.rb $VID_DIR) \
-filter_complex \
"[0:v]crop=180:50:0:in_h-out_h,boxblur=10[fg]; \
[0:v][fg]overlay=0:main_h-overlay_h[v]" \
-map "[v]" \
-s 1920x1080 \
-preset fast \
-crf 28 \
-an \
-c:v libx264 \
-x264-params "nal-hrd=cbr" \
-pix_fmt yuv420p \
-r 30 \
-minrate 850k -maxrate 850k -b:v 900k -bufsize 280k \
-force_key_frames 'expr:gte(t,n_forced*2)' \
-f flv \
"rtmp://live.twitch.tv/app/$STREAM_KEY"
####################################################################
# safe to put old configs down here cause we wont ever get down here
####################################################################
exit 0
# these are other options we could try/have tried
-r 30 \
-maxrate 12M \
-bufsize 6M \
-framerate 15 \
-preset slow \
-g 60 \
-preset slower \
-c:v mpeg2video \
-loglevel info \
-vcodec copy \
-b:v 1M \
-minrate 1M \
-maxrate 1M \
-bufsize 2M \
-profile:v baseline \
-level 3.0 \
-movflags +faststart \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment