fosdem-recorder.cr/src/controllers/download_controller.cr

59 lines
1.6 KiB
Crystal
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

require "./base_controller"
require "../utils/url_validator"
module FosdemRecorder
class DownloadController < BaseController
def self.process(url)
UrlValidator.validate_event! url
meta = EventPage.get_meta(url)
now = Time.local #Time::Location.load("Europe/Brussels")
event_start = meta.event_start
raise "Event start is missing!" if event_start.nil?
event_stop = meta.event_stop
raise "Event stop is missing!" if event_stop.nil?
event_start_localtime = event_start.to_local
postpone_download = (event_start_localtime >= now)
# compute remaining duration when needed
duration = Duration.new(start: event_start, stop: event_stop)
remaining_duration = duration.from_now
timeformat = event_start_localtime.to_s("%H:%M %Y-%m-%d")
# FIXME: mark the file as partial
cmd = [
"ffmpeg",
# First the stream URL
"-i", meta.stream_url,
# Then the codec (simple copy)
"-c", "copy",
# Fix malformed AAC bitstream when detected
"-bsf:a", "aac_adtstoasc",
# Make the stream playable as we download it
"-movflags", "frag_keyframe+empty_moov+default_base_moof+faststart",
# Set record duration
"-t", remaining_duration.to_s,
# Set output filename
"\"#{meta.title_sanitized}.mp4\""
].join(" ")
if postpone_download
cmd = "echo #{cmd} | at #{timeformat}"
else
cmd = "echo #{cmd} | at now"
end
puts "Command: #{cmd}".colorize.fore(:yellow)
system cmd
end
end
end