Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rake task to prepare MP3 files for Apple's HTTP Streaming protocol and upload them to S3
namespace :audio do
desc "downsample, segment, and upload audio files"
task :load => :environment do
access_key_id = ENV['AWS_ACCESS_KEY_ID']
secret_access_key = ENV['AWS_ACCESS_KEY']
source_dir = ENV['SOURCE_DIR']
fail "Must specify AWS_ACCESS_KEY_ID, AWS_ACCESS_KEY, and SOURCE_DIR" if access_key_id.blank? || secret_access_key.blank? || source_dir.blank?
filenames = Dir["#{source_dir}/**/**.*"]
fail "#{source_dir} is empty" if filenames.empty?
require "shellwords"
require 'cgi'
today = Date.today.to_s
dest_dir = "#{Rails.root}/tmp"
s3_dest = "files.example.com/audio"
prog_urls = []
mp3_paths = []
filenames.each do |filename|
new_filename = File.basename(filename.gsub('\s-\s','-').gsub(/\s+/,'-'),".mp3")
subdir = "#{today}/#{new_filename}"
dir = "#{dest_dir}/#{subdir}"
FileUtils.mkdir_p(dir)
complete_file = "#{dir}/#{new_filename}.mp3"
cmd = "ffmpeg -i '#{filename}' -ab 64k -acodec libmp3lame '#{complete_file}'"
system cmd
cmd = "/usr/bin/mediafilesegmenter -f #{dir} #{Shellwords.escape(complete_file)}"
system cmd
prog_urls << "http://#{s3_dest}/#{subdir}/prog_index.m3u8"
mp3_paths << "#{subdir}/#{CGI.escape(File.basename(complete_file))}"
end
cmd = "s3cmd put -H -M --progress --recursive --acl-public #{dest_dir}/#{today} s3://#{s3_dest}/ 2>&1"
system(cmd)
prog_urls.each do |url|
cmd = "mediastreamvalidator validate #{url} 2>&1"
system cmd
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.