Skip to content

Instantly share code, notes, and snippets.

@dmdeller
Last active December 11, 2015 21:18
Show Gist options
  • Select an option

  • Save dmdeller/4661182 to your computer and use it in GitHub Desktop.

Select an option

Save dmdeller/4661182 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/http'
require 'digest/sha2'
require 'fileutils'
require 'date'
show = ARGV[0]
case show
when 'colbert'
base_url = 'http://www.colbertnation.com/full-episodes/'
base_filename = 'The.Colbert.Report.'
when 'dailyshow'
base_url = 'http://www.thedailyshow.com/full-episodes/'
base_filename = 'The.Daily.Show.with.Jon.Stewart.'
else
raise "Unknown show: #{show}"
end
response = Net::HTTP.get_response(URI(base_url))
episode_url = response['Location']
date_part = episode_url.match(/\/full-episodes\/([a-z]+-[a-z]+-[0-9]+-[0-9]+).*/i)[1].gsub('-', ' ')
date = Date.parse(date_part)
hash = Digest::SHA256.hexdigest(episode_url)
if (date.nil?)
output_filename = "#{base_filename}#{hash}.mp4"
else
output_filename = "#{base_filename}#{date.strftime('%Y.%m.%d')}.mp4"
end
if (File.exists?(output_filename))
raise "File already exists: #{output_filename}"
end
begin
FileUtils.mkdir(hash)
rescue Errno::EEXIST
end
FileUtils.cd(hash) do
system("youtube-dl \"#{episode_url}\"")
mp4s = Dir.entries('.').reject do |file|
!file.match(/\.mp4$/)
end
if (mp4s.count == 0)
raise "No videos..."
end
mp4s.sort!
tsses = []
mp4s.each do |mp4|
ts = mp4.gsub(/.mp4$/, '.ts')
command = "ffmpeg -y -i #{mp4} -c copy -bsf h264_mp4toannexb #{ts}"
print "--> #{command}\n"
system(command)
tsses << ts
end
tsses_flat = tsses.join('|')
command = "ffmpeg -y -i \"concat:#{tsses_flat}\" -c copy -absf aac_adtstoasc \"../#{output_filename}\""
print "--> #{command}\n"
system (command)
FileUtils.rm(mp4s)
FileUtils.rm(tsses)
end
FileUtils.rmdir(hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment