Skip to content

Instantly share code, notes, and snippets.

@tanookiben
Created November 9, 2017 23:43
Show Gist options
  • Save tanookiben/4cad99c7b29c033926baf468e2f0ceaf to your computer and use it in GitHub Desktop.
Save tanookiben/4cad99c7b29c033926baf468e2f0ceaf to your computer and use it in GitHub Desktop.
Playlist Downloader

Playlist Downloader

Download VODs of stuff with playlist files and base URLs.

Download

download.rb - Download .ts files

Expects a directory structure:

/download.rb /directoryA /playlistA1.m3u8 /playlistA2.m3u8 /directoryB /playlistB1.m3u8

Files will be downloaded into new folders defined by the name of the playlist file:

/download.rb /directoryA /playlistA1 /00000.ts /00001.ts /00002.ts /00003.ts /00004.ts /00005.ts /playlistA1.m3u8

Process

process.rb - Processes .ts files

Expects a similar directory structure for the download step. Requires ffmpeg to be available.

  1. cat all .ts files into a master.ts file named appropriately by the directory
  2. ffmpeg convert the master .ts file into its corresponding .mp4 file
#!/usr/bin/env ruby
SKIP_DIRS = ['.', '..', '.DS_Store']
RUBY_EXT = ".rb"
PLAYLIST_EXT = ".m3u8"
dirs = Dir.entries('.')
dirs.each do |dir|
next if SKIP_DIRS.include?(dir)
next if File.extname(dir) == RUBY_EXT
puts "Processing directory \"#{dir}\""
files = Dir.entries(dir)
files.each do |file|
if File.extname(file) == PLAYLIST_EXT
puts "Processing file \"#{file}\""
name = File.basename(file, PLAYLIST_EXT)
if !Dir.exists?("#{dir}/#{name}")
system("mkdir \"#{dir}/#{name}\"")
f = File.open("#{dir}/#{file}", "r")
c = 0
f.each_line do |l|
if @base.nil?
@base = l.strip
next
end
if l.strip.match(/(v\d_\d+\.ts.*)/)
res = $1
if !res.nil?
system("wget --quiet -O \"#{dir}/#{name}/#{c.to_s.rjust(5, "0")}.ts\" --header='user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36' \"#{@base}#{res}\"")
c += 1
end
end
end
@base = nil
else
puts "Skipping #{file}, directory exists"
end
end
end
end
#!/usr/bin/env ruby
SKIP_DIRS = ['.', '..', '.DS_Store']
ALLOWED_DIRS = []
RUBY_EXT = ".rb"
PLAYLIST_EXT = ".m3u8"
VIDEO_EXT = ".mp4"
Dir.entries('.').each do |dir|
next if !ALLOWED_DIRS.include?(dir)
next if SKIP_DIRS.include?(dir)
next if File.extname(dir) == RUBY_EXT
puts "Processing directory \"#{dir}\""
Dir.entries(dir).each do |title|
next if File.extname(title) == PLAYLIST_EXT
next if File.extname(title) == VIDEO_EXT
next if SKIP_DIRS.include?(title)
system("pwd")
Dir.chdir("#{dir}/#{title}") do
system("pwd")
system("cat *.ts > master.ts")
system("ffmpeg -i master.ts -acodec copy -vcodec copy \"#{title}.mp4\"")
system("mv \"#{title}.mp4\" ..")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment