Skip to content

Instantly share code, notes, and snippets.

@Coro365

Coro365/auto_encode.rb

Last active Oct 25, 2017
Embed
What would you like to do?
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
end
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}")
end
# read encode log
FileUtils.touch(WD+"/encode.log") unless File.exist?(WD+"/encode.log")
encode_log = File.open(WD+"/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.")
else
puts ("Start encode!")
encode (ts)
File.open(WD+"/encode.log","a") do |f|
f.puts(Time.now.to_s+"\t"+ts_name)
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.