Class: Scissor::Writer
Defined Under Namespace
Classes: CommandFailed, EmptyFragment, Error, FileExists
Instance Method Summary
collapse
Methods included from Loggable
#logger
Constructor Details
#initialize ⇒ Writer
Returns a new instance of Writer.
13
14
15
16
17
18
|
# File 'lib/scissor/writer.rb', line 13
def initialize
@tracks = []
which('ecasound')
which('ffmpeg')
end
|
Instance Method Details
#add_track(fragments) ⇒ Object
20
21
22
|
# File 'lib/scissor/writer.rb', line 20
def add_track(fragments)
@tracks << fragments
end
|
#fragments_to_file(fragments, outfile, tmpdir) ⇒ Object
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
|
# File 'lib/scissor/writer.rb', line 24
def fragments_to_file(fragments, outfile, tmpdir)
position = 0.0
cmd = %w/ecasound/
fragments.each_with_index do |fragment, index|
fragment_filename = fragment.filename
fragment_duration = fragment.duration
if !index.zero? && (index % 80).zero?
run_command(cmd.join(' '))
cmd = %w/ecasound/
end
fragment_outfile =
fragment_filename.extname.downcase == '.wav' ? fragment_filename :
tmpdir + (Digest::MD5.hexdigest(fragment_filename) + '.wav')
unless fragment_outfile.exist?
run_command("ffmpeg -i \"#{fragment_filename}\" \"#{fragment_outfile}\"")
end
cmd <<
"-a:#{index} " +
"-i:" +
(fragment.reversed? ? 'reverse,' : '') +
"select,#{fragment.start},#{fragment.true_duration},\"#{fragment_outfile}\" " +
"-o:#{outfile} " +
(fragment.pitch.to_f == 100.0 ? "" : "-ei:#{fragment.pitch} ") +
"-y:#{position}"
position += fragment_duration
end
run_command(cmd.join(' '))
end
|
#mix_files(filenames, outfile) ⇒ Object
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/scissor/writer.rb', line 60
def mix_files(filenames, outfile)
cmd = %w/ecasound/
filenames.each_with_index do |tf, index|
cmd << "-a:#{index} -i:#{tf}"
end
cmd << "-a:all -o:#{outfile}"
run_command(cmd.join(' '))
end
|
#run_command(cmd) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/scissor/writer.rb', line 113
def run_command(cmd)
logger.debug("run_command: #{cmd}")
result = ''
status = Open4.popen4(cmd) do |pid, stdin, stdout, stderr|
logger.debug(stderr.read)
result = stdout.read
end
if status.exitstatus != 0
raise CommandFailed.new(cmd)
end
return result
end
|
#to_file(filename, options) ⇒ Object
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
|
# File 'lib/scissor/writer.rb', line 71
def to_file(filename, options)
filename = Pathname.new(filename)
if @tracks.flatten.empty?
raise EmptyFragment
end
options = {
:overwrite => false
}.merge(options)
if filename.exist?
if options[:overwrite]
filename.unlink
else
raise FileExists
end
end
TempDir.create do |dir|
tmpdir = Pathname.new(dir)
tmpfiles = []
@tracks.each_with_index do |fragments, track_index|
tmpfiles << tmpfile = tmpdir + 'track_%s.wav' % track_index.to_s
fragments_to_file(fragments, tmpfile, tmpdir)
end
mix_files(tmpfiles, final_tmpfile = tmpdir + 'tmp.wav')
if filename.extname == '.wav'
File.rename(final_tmpfile, filename)
else
run_command("ffmpeg -i \"#{final_tmpfile}\" \"#{filename}\"")
end
end
end
|
#which(command) ⇒ Object
109
110
111
|
# File 'lib/scissor/writer.rb', line 109
def which(command)
run_command("which #{command}")
end
|