Skip to content

Instantly share code, notes, and snippets.

Last active October 25, 2017 16:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Coro365/e718409a68d920ffd96f2f052cfabfea to your computer and use it in GitHub Desktop.
Encode the recorded m2ts file
require 'fileutils'
require 'shellwords'
WD = File.dirname(File.expand_path(__FILE__))
TSDIR = "path"
MP4DIR = "path"
def recodeing? (ts)
s1 = File.size(ts)
sleep 1
s2 = File.size(ts)
return true if s1 < s2
return false if s1 == s2
def encode (ts)
ts_shl = Shellwords.escape(ts)
mp4_shl = Shellwords.escape(MP4DIR + "/" + File.basename(ts, ".m2ts") + ".mp4")
option = "-threads 4 -vcodec libx264 -ar 48000 -ab 128k -r 30000/1001 -vsync 1 -deinterlace -f mp4 -bufsize 20000k -maxrate 25000k"
system("ffmpeg -i #{ts_shl} #{option} #{mp4_shl}")
# read encode log
FileUtils.touch(WD+"/encode.log") unless File.exist?(WD+"/encode.log")
encode_log ="/encode.log","r").read
# read recode dirctory files
Dir.glob(TSDIR+"/*.m2ts") do |ts|
ts_name = File.basename(ts)
# Skip encoded
next if encode_log.include?(ts_name)
puts ("Find new ts file #{ts_name}")
if recodeing? (ts)
puts ("Recodeing ts skip.")
puts ("Start encode!")
encode (ts)"/encode.log","a") do |f|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment