Skip to content

Instantly share code, notes, and snippets.

@shunirr
Created March 4, 2016 08:36
Show Gist options
  • Select an option

  • Save shunirr/d33fc55ed7cb9f93de1a to your computer and use it in GitHub Desktop.

Select an option

Save shunirr/d33fc55ed7cb9f93de1a to your computer and use it in GitHub Desktop.

Revisions

  1. shunirr created this gist Mar 4, 2016.
    28 changes: 28 additions & 0 deletions hls_capture.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/usr/bin/env ruby

    require "open3"

    base_url = ARGV.shift
    hls_file = ARGV.shift

    o, e, s = Open3.capture3 "curl #{base_url}#{hls_file}"
    tslist = o.split("\n").delete_if{|i| i =~ /^#/ }

    v_cmd = "ffmpeg "
    a_cmd = "ffmpeg "
    tslist.each do |ts|
    Open3.capture2e "curl -O #{base_url}#{ts}"
    Open3.capture2e "ffmpeg -i #{ts} -vcodec copy -an #{ts}.mp4"
    Open3.capture2e "ffmpeg -i #{ts} -acodec copy -vn #{ts}.aac"
    v_cmd += "-i #{ts}.mp4 "
    a_cmd += "-i #{ts}.aac "
    end

    Open3.capture2e "#{v_cmd} -filter_complex 'concat=n=#{tslist.size}:v=1:a=0' video.mp4"
    Open3.capture2e "#{a_cmd} -filter_complex 'concat=n=#{tslist.size}:v=0:a=1' audio.aac"
    Open3.capture2e "ffmpeg -i video.mp4 -i audio.aac #{Time.now.to_i}.mp4"
    Open3.capture2e "rm video.mp4 audio.aac"
    tslist.each do |ts|
    Open3.capture2e "rm #{ts} #{ts}.mp4 #{ts}.aac"
    end