Last active
January 15, 2016 00:03
-
-
Save lune-sta/4042170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: SJIS -*- | |
require 'fileutils' | |
TMP_DIR = "D:\\TEMP\\#{$$}\\" | |
DGINDEX = "C:\\dtv\\dgmpgdec158\\DGIndex.exe" | |
TC2AAC = "C:\\dtv\\ts2aac\\ts2aac.exe" | |
LOGOG = "C:\\dtv\\logoGuillo\\logoGuillo.exe" | |
LOGO_DIR = "C:\\dtv\\logoGuillo\\logo\\" | |
AVS2X = "C:\\dtv\\logoGuillo\\avs2pipemod.exe" | |
AVSPLG = "C:\\dtv\\dgmpgdec158\\DGDecode.dll" | |
AVS2WAV = "C:\\dtv\\bin\\avs2wav.exe" | |
NEROAE = "C:\\dtv\\bin\\neroAacEnc.exe" | |
AACGAIN = "C:\\dtv\\bin\\aacgain.exe" | |
AACEDIT = "C:\\dtv\\bin\\aacedit2.exe" | |
MP4BOX = "C:\\dtv\\bin\\MP4Box.exe" | |
FAWCL = "C:\\dtv\\bin\\fawcl.exe" | |
AUTOVFR = "C:\\dtv\\AutoVfr\\AutoVfr.exe" | |
VFR_AVS = "C:\\dtv\\Liatris\\Auto_Vfr.avs" | |
AVS2AVI = "C:\\dtv\\bin\\avs2avi.exe" | |
X264 = "C:\\dtv\\bin\\x264.exe" | |
SAVE_DIR = "N:\\movie\\" | |
CAP2ASS = "C:\\dtv\\Caption2Ass_PCR\\Caption2Ass_PCR.exe" | |
CHPTEXE = "C:\\dtv\\CMcut\\chapter_exe.exe" | |
LOGOFRAME = "C:\\dtv\\CMcut\\logoframe.exe" | |
LOGOSCP = "C:\\dtv\\CMcut\\join_logo_scp.exe" | |
CUTRULE = "C:\\dtv\\CMcut\\standard.txt" | |
SCRENAME = "C:\\dtv\\SCRename\\SCRename.vbs" | |
TWEET = "C:\\dtv\\Liatris\\tweet.rb" | |
require TWEET; | |
start_time = Time.now | |
file = ARGV[0] | |
dpn = File.basename(file, ".ts") | |
ext = File.extname(file) | |
if ext != ".ts" | |
puts "tsファイルではありません." | |
exit | |
end | |
dpn =~ /^(.*?) (第.*話\s?「.*?」) \((.*?)\)/ | |
title = $1 | |
subtitle = $2 | |
ch = $3 | |
puts "タイトル #{title}" | |
puts "サブタイトル #{subtitle}" | |
puts "チャンネル #{ch}" | |
Tweet.new("#ricca #rec #p#{$$} #{title} #{subtitle} ##{ch} の録画後処理を始めます。") | |
FileUtils.mkdir_p(TMP_DIR) unless FileTest.exist?(TMP_DIR) | |
Dir::chdir(TMP_DIR) | |
FileUtils.rm_r(Dir.glob( '.' ), {:force=>true}) | |
puts "#{TMP_DIR}を作成しました." | |
puts "#{file}をエンコードします." | |
if file.empty? | |
puts "ファイルが指定されていません" | |
exit | |
end | |
puts "ファイルをコピーします..." | |
FileUtils.cp(file, "tmp.ts") | |
puts "字幕の作成..." | |
#puts `#{CAP2ASS} -format dual tmp.ts` | |
puts "D2Vの抽出..." | |
puts `#{DGINDEX} -IF=[tmp.ts] -OF=[tmp] -exit` | |
puts "AACの抽出..." | |
puts result = `#{TC2AAC} -i tmp.ts -o tmp -B` | |
delay = result.scan(/ディレイ:(.*)ms/).flatten[0].to_f | |
aac_file = result.scan(/^outfile:(.*)$/).flatten[0] | |
re_file = file.tr('A-Z','A-Z').tr('a-z','a-z').tr('0-9','0-9') | |
puts "CMカット用AVSファイルの作成..." | |
avs = open("cmcut.avs", 'a') | |
avs.puts <<-EOS | |
video = MPEG2Source("#{TMP_DIR}tmp.d2v") | |
audio = bassAudioSource("#{TMP_DIR}#{aac_file}") | |
AudioDub(video, audio) | |
DelayAudio(#{sprintf("%.3f", delay/1000)}) | |
EOS | |
avs.close | |
puts "音声出力..." | |
puts `#{AVS2WAV} cmcut.avs cmcut.wav` | |
puts "無音区間検出..." | |
puts `#{CHPTEXE} -v "#{TMP_DIR}cmcut.avs" -a "cmcut.wav" -o "#{TMP_DIR}chapter_exe.txt"` | |
puts "ロゴ検出(#{ch})..." | |
#puts `#{LOGOG} -video "#{TMP_DIR}tmp.d2v" -lgd "#{LOGO_DIR}#{ch}.lgd" -prm "#{LOGO_DIR}#{ch}.lgd.autoTune.param" -avs2x "#{AVS2X}" -avsPlg "#{AVSPLG}" -out "tmp.d2v.txt" -agrPstFlt -outFmt trim -strict` | |
puts `#{LOGOFRAME} "#{TMP_DIR}cmcut.avs" -logo "#{LOGO_DIR}#{ch}.lgd" -oa "logoframe.txt"` | |
puts `#{LOGOSCP} -inlogo "logoframe.txt" -inscp "#{TMP_DIR}chapter_exe.txt" -incmd "#{CUTRULE}" -o "logoscp.avs"` | |
trim = "" | |
cmc = open("logoscp.avs") | |
trim = cmc.read | |
cmc.close | |
Tweet.new("#ricca #rec #p#{$$} CM解析が終わりました。 Trim#{trim.chomp.scan(/(\(\d+,\d+\))/).join('+')}") | |
puts "CMカット用AVSファイルの作成..." | |
avs = open("tmp.avs", 'a') | |
avs.puts <<-EOS | |
video = MPEG2Source("#{TMP_DIR}tmp.d2v") | |
audio = bassAudioSource("#{TMP_DIR}#{aac_file}") | |
AudioDub(video, audio) | |
DelayAudio(#{sprintf("%.3f", delay/1000)}) | |
#{trim} | |
EOS | |
avs.close | |
puts "自動VFR用AVSファイルの作成..." | |
FileUtils.cp(VFR_AVS, "Auto_Vfr.avs") | |
FileUtils.cp("tmp.avs", "AutoVfr01.avs") | |
vfr1 = open("AutoVfr01.avs", 'a') | |
vfr1.puts <<-EOS | |
Import("Auto_Vfr.avs") | |
blank=BlankClip(width=450,height=250) | |
Overlay(blank,x=100,y=20,opacity=1.0) | |
Auto_VFR("log_file",IsCrop=false,IsDup=false) | |
return last | |
EOS | |
vfr1.close | |
FileUtils.cp("tmp.avs", "AutoVfr02.avs") | |
vfr2 = open("AutoVfr02.avs", 'a') | |
vfr2.puts <<-EOS | |
its(def="AutoVFR.def", fps=-1, output="encode_AutoVFR.tmc") | |
ConvertToYV12 | |
LanczosResize(1280,720) | |
WarpSharp(95,3,85,0) | |
UnsharpMask(32,8,38) | |
return last | |
function TIVTC24P2(clip clip){ | |
Interp = clip.AssumeTFF().SeparateFields().SelectEven().eedi3(dh=true) | |
Deinted=clip.TDeint(order=1,field=1,edeint=Interp) | |
clip = clip.TFM(mode=6,order=1,PP=7,slow=2,mChroma=true,clip2=Deinted) | |
clip = clip.TDecimate(mode=1) | |
return clip | |
} | |
EOS | |
vfr2.close | |
puts "音声出力..." | |
puts `#{AVS2WAV} tmp.avs tmp.wav` | |
puts "音声のエンコード..." | |
puts `#{NEROAE} -ignorelength -lc -br 192000 -2pass -if tmp.wav -of tmp.m4a` | |
puts "音量調整..." | |
puts `#{AACGAIN} /r /a /c tmp.m4a` | |
puts `#{AACGAIN} /g 2.0 tmp.m4a` | |
puts "AAC処理..." | |
puts `#{AACEDIT} -d #{delay.to_i} -t tmp.avs -o tmp_trim.aac "#{aac_file}"` | |
puts `#{MP4BOX} -mpeg4 -add tmp_trim.aac -new tmp_trim.m4a` | |
puts "偽装WAV作成..." | |
FileUtils.cp(aac_file, "cp.aac") | |
puts `#{FAWCL} -s2 -d#{delay} cp.aac faw.wav` | |
faw = open("faw.avs", 'a') | |
faw.puts <<-EOS | |
video = MPEG2Source("#{TMP_DIR}tmp.d2v") | |
audio = WavSource("#{TMP_DIR}faw.wav") | |
AudioDub(video, audio) | |
#{trim} | |
EOS | |
faw.close | |
puts "音声のTrim..." | |
puts `#{AVS2WAV} faw.avs faw_trim.wav` | |
puts "偽装WAVを復元..." | |
puts `#{FAWCL} faw_trim.wav pd.aac` | |
puts `#{MP4BOX} -mpeg4 -add pd.aac -new pd.m4a` | |
puts "自動VFR用のログを作成..." | |
puts `#{AVS2AVI} #{TMP_DIR}AutoVfr01.avs -c null -o n` | |
puts "ItsのDefファイル出力..." | |
puts `#{AUTOVFR} -i "log_file" -o AutoVFR.def -skip 3 -30f 0 -60f 1` | |
puts "映像のエンコード..." | |
puts `#{X264} --crf 21 --level 4.1 --keyint 300 --min-keyint 1 --scenecut 65 --bframes 3 --8x8dct --trellis 2 --ref 4 --threads 12 --mixed-refs --subme 6 --no-fast-pskip --sar 1:1 --no-dct-decimate --tcfile-in "encode_AutoVFR.tmc" --timebase 1001/120000 --output tmp.mp4 #{TMP_DIR}AutoVfr02.avs` | |
puts "映像と音声を結合..." | |
sav = "#{SAVE_DIR}#{title}\\" # 保存先 | |
FileUtils.mkdir_p(sav) unless FileTest.exist?(sav) | |
puts `#{MP4BOX} -add tmp.mp4 -add pd.m4a -new "#{sav}#{title} #{subtitle}.mp4"` | |
end_time = Time.now | |
Dir::chdir("..") | |
FileUtils.rm_r(TMP_DIR) | |
if FileTest.exist?("#{sav}#{title} #{subtitle}.mp4") | |
Tweet.new("#ricca #rec #p#{$$} VFRエンコードが正常に終了しました。(size = #{FileTest.size("#{sav}#{title} #{subtitle}.mp4") / (1024*1024)}MB") | |
Tweet.new(".@lune194 #ricca #rec ##{$$} #{title} #{subtitle} ##{ch} の視聴準備ができました。") | |
puts "エンコードが正常に成功しました. (size = #{FileTest.size("#{sav}#{title} #{subtitle}.mp4") / (1024*1024)}MB, 処理時間 #{(end_time - start_time).to_s}s)" | |
#result = `#{TWEET} "#{title} #{subtitle}のエンコードが終了しました (エンコード後サイズ: #{FileTest.size("#{sav}#{title} #{subtitle}.mp4") / (1024*1024)}MB, 処理時間: #{(end_time - start_time).to_s}s)"` | |
#FileUtils.rm(file) | |
else | |
Tweet.new(".@lune194 #ricca #rec #p#{$$} #{title} #{subtitle} ##{ch} の録画後処理に失敗しました。") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment