Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Split ogv video with mplayer and mencoder.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
if ARGV.length < 3
puts "#{$0}: usage: #{$0} src destdir [sec]"
exit
end
# 引数の取得
src,destdir,sec_per_part = ARGV
sec_per_part = (sec_per_part.nil?) ? 60 * 5: sec_per_part.to_i
puts "src=#{src}, destdir=#{destdir}, sec_per_part=#{sec_per_part}"
# TODO src, dest のチェック
# src はビデオファイル
# destdir はディレクトリ
# 時間の取得
LENGTH = `mplayer -vo null -ao null -frames 0 -identify #{src} 2>/dev/null | grep "ID_LENGTH"`.gsub("ID_LENGTH=", "").to_i + 1
puts "video length: #{LENGTH} sec"
# ファイルの分割実行
(LENGTH / sec_per_part + 1).times do |i|
dest = "#{destdir}/#{File.basename(src,'.*')}.#{sprintf('%03d', i+1)}#{File.extname(src)}.avi"
ss, ee = i * sec_per_part, sec_per_part
startpos = sprintf("%02d:%02d:%02d",ss / 60 / 60, ss / 60 % 60, ss % 60)
endpos = sprintf("%02d:%02d:%02d",ee / 60 / 60, ee / 60 % 60, ee % 60)
puts "mencoder -ss #{startpos} -endpos #{endpos} -oac copy -ovc copy #{src} -o #{dest}"
system "mencoder -ni -fps 30 -ofps 30 -ss #{startpos} -endpos #{endpos} -oac mp3lame -ovc lavc #{src} -o #{dest}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment