blackjid
4/23/2014 - 9:28 PM

Vj Archive test for rhinobird

Vj Archive test for rhinobird

def prepare
    # Download the files
    for stream in @stream_urls do
      system("curl -O https://s3.amazonaws.com/media-rhinobird.tv/#{stream[:name]}") unless File.exists?(stream[:name])
    end

    # Create a tmp dir
    system("rm -rf tmp && mkdir tmp")
    system("rm -f video_story.txt")
    system("rm -f audio_story.txt")
    system("rm -f vj_audio.ogg")
    system("rm -f vj_video.webm")
    system("rm -f vj_story.webm")
end

# Get the vj story object
def get_vj_item (index, stream, start_time, length)
    {
        index: index,
        stream: stream,
        source: @stream_urls[index][:name],
        duration: @stream_urls[index][:duration],
        start_time: start_time,
        length: length
      }
end

def demux (vj_item)
    name = File.basename(vj_item[:source], File.extname(vj_item[:source]))

    # FFMPEG commands
    ffmpeg_demux_video = "ffmpeg -ss #{vj_item[:start_time]} -t #{vj_item[:length]} -i #{vj_item[:source]} -y -codec:v copy -an tmp/#{name}-#{vj_item[:index]}.webm"
    ffmpeg_demux_audio = "ffmpeg -ss #{vj_item[:start_time]} -t #{vj_item[:length]} -i #{vj_item[:source]} -y -codec:a copy -vn tmp/#{name}-#{vj_item[:index]}.ogg"

    # Execute the commands
    puts vj_item[:stream]
    system(ffmpeg_demux_video) if vj_item[:stream] == :video
    system(ffmpeg_demux_audio) if vj_item[:stream] == :audio
end

def write_concat_file(type, story)
    File.open("#{type}_story.txt", 'a') do |f|
        for vj_item in @vj_story.select {|vs| vs[:stream] == type} do
            name = File.basename(vj_item[:source], File.extname(vj_item[:source]))

            f.write("file 'tmp/#{name}-#{vj_item[:index]}.webm'\n") if type == :video
            f.write("file 'tmp/#{name}-#{vj_item[:index]}.ogg'\n") if type == :audio
        end
    end
end

def concat ()
    system("ffmpeg -f concat -i video_story.txt -c copy vj_video.webm") if File.exists? "video_story.txt"
    system("ffmpeg -f concat -i audio_story.txt -c copy vj_audio.ogg") if File.exists? "audio_story.txt"
end

def mux ()
    system("ffmpeg -i vj_video.webm -i vj_audio.ogg -c copy vj_story.webm")
end

###############

# This are the examples to use
@stream_urls = [
  { name: "recording160556248156353820.webm", duration: "00:06:33.04" },
  { name: "recording551125586498528700.webm", duration: "00:02:15.97" },
  { name: "recording368509666994214100.webm", duration: "00:09:11.47" }
]

# A handmade vj story
@vj_story = [
    get_vj_item(2, :video, "00:00:00", "00:01:00"),
    get_vj_item(0, :video, "00:00:02", "00:00:25"),
    get_vj_item(1, :video, "00:00:02", "00:00:20"),
    get_vj_item(0, :audio, "00:00:00", "00:00:20"),
    get_vj_item(1, :audio, "00:00:00", "00:00:20"),
    get_vj_item(2, :audio, "00:00:00", "00:00:20"),
]

# prepare the env
prepare

# Demux all the story items
for vj_item in @vj_story do
    demux vj_item
end

# # Join stories
write_concat_file(:video, @vj_story)
write_concat_file(:audio, @vj_story)

# # concat each stream (audio and video)
concat

# # Mux the video and audio into one final video
mux