-
Notifications
You must be signed in to change notification settings - Fork 3
/
vegas-to-sresync.rb
executable file
·108 lines (87 loc) · 3.45 KB
/
vegas-to-sresync.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#! /usr/bin/env ruby
#
# == Synopsis
#
# vegas-to-sresync: converts Sony Vegas EDL text files (exported projects in CSV format)
# into srt-resync.rb input file format
#
# == Usage
#
# ruby vegas-to-sresync.rb [-f 24000/1001] exported_project.csv [track_1_name track_2_name ...]
#
# -f, --framerate:
# override the default framerate of 24000/1001; acceptable formats are 123.45 and 987/654
require 'getoptlong'
require 'csv'
require 'rdoc/usage'
VEGAS_TO_SRESYNC_VERSION = '1.0'
def die s
puts "Fatal error: #{s}"
exit 1
end
class Track
attr_reader :previous_end_time, :file_name, :skipped_ranges
def initialize file_name
@file_name = file_name
@previous_end_time = 0.0
@skipped_ranges = []
end
def add_range! start_time, length
if start_time > @previous_end_time
@skipped_ranges << [@previous_end_time, start_time]
end
@previous_end_time = start_time + length
end
end
begin
RDoc::usage if ARGV.size < 1
opts = GetoptLong.new(
[ '--framerate', '-f', GetoptLong::REQUIRED_ARGUMENT ]
)
frame_rate = '24000/1001'
opts.each do |opt, arg|
case opt
when '--framerate'
frame_rate = arg.strip
end
end
input_file = ARGV[0]
subtitle_names = ARGV[1 .. -1]
if frame_rate =~ %r!^(\d+)/(\d+)$!
frame_rate_num = $1.to_i
frame_rate_den = $2.to_i
elsif frame_rate =~ /^([\d.]+)$/
frame_rate_num = $1.to_f
frame_rate_den = 1
else
die %Q,invalid frame rate format: "#{frame_rate}",
end
File.file? input_file or die "input file not found: #{input_file}"
tracks = {}
CSV.parse(File.read(input_file).gsub('; ', ';'), ?;) do |row|
next if row[1] =~ /Track/
track = row[1]
start_time = row[2].to_f
length = row[3].to_f
file_name = File.basename(row[11].gsub('\\', '/'))
track = (tracks[track] ||= Track.new(file_name))
track.add_range! start_time, length
end
max_end_time = tracks.values.collect { |t| t.previous_end_time }.max
tracks.values.each { |t| t.add_range! max_end_time, 0.0 }
if subtitle_names.empty?
common_prefix = 0; common_suffix = 0
common_prefix += 1 while tracks.values.all? { |t| t.file_name[0 .. common_prefix].upcase == tracks.values.first.file_name[0 .. common_prefix].upcase }
common_suffix += 1 while tracks.values.all? { |t| t.file_name[-common_suffix-1 .. -1].upcase == tracks.values.first.file_name[-common_suffix-1 .. -1].upcase }
# puts tracks.values.first.file_name[0 .. common_prefix-1]
# puts tracks.values.first.file_name[-common_suffix .. -1]
# puts tracks.values.first.file_name[common_prefix .. -common_suffix-1]
elsif subtitle_names.size != tracks.size
puts "The number of subtitle track names specified (#{subtitle_names.size}) does not match the actual number of tracks (#{tracks.size}). Stop."
exit 1
end
max_len = if subtitle_names.empty? then tracks.values.collect { |t| t.file_name[common_prefix .. -common_suffix-1] } else subtitle_names end.collect { |name| name.length }.max
puts "#{frame_rate_num}/#{frame_rate_den}"
puts
tracks.values.each_with_index { |t, index| puts sprintf("%-#{max_len+1}s", if subtitle_names.empty? then t.file_name[common_prefix .. -common_suffix-1] else subtitle_names[index] end) + " " + if t.skipped_ranges.empty? then "0+0" else t.skipped_ranges.collect { |r| [(r[0]*1.0*frame_rate_num/frame_rate_den/1000).round, (r[1]*1.0*frame_rate_num/frame_rate_den/1000).round] }.collect { |b,e| "#{b}+#{e-b}"}.join(", ") end }
end