Class: Scissor::VideoChunk

Inherits:
Chunk
  • Object
show all
Defined in:
lib/scissor-video/chunk.rb

Instance Method Summary collapse

Methods inherited from Chunk

#+

Constructor Details

#initialize(filename = nil) ⇒ VideoChunk

Returns a new instance of VideoChunk.



15
16
17
18
19
20
21
22
23
24
# File 'lib/scissor-video/chunk.rb', line 15

def initialize(filename = nil)
  @fragments = []

  if filename
    @fragments << Fragment.new(
      filename,
      0,
      VideoFile.new(filename).length)
  end
end

Instance Method Details

#to_file(filename, options = {}) ⇒ Object



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
# File 'lib/scissor-video/chunk.rb', line 26

def to_file(filename, options = {})
  filename = Pathname.new(filename)

  if @fragments.empty?
    raise EmptyFragment
  end

  options = {
    :overwrite => false,
    :save_workfiles => false
  }.merge(options)

  filename = Pathname.new(filename)

  if filename.exist?
    if options[:overwrite]
      filename.unlink
    else
      raise FileExists
    end
  end

  concat_files = []
  ffmpeg = Scissor.ffmpeg

  position = 0.0
  tmpdir = ffmpeg.work_dir
  tmpfile = tmpdir + 'tmp.avi'

  begin
    @fragments.each_with_index do |fragment, index|
      fragment_filename = fragment.filename
      fragment_duration = fragment.duration

      fragment_tmpfile = tmpdir + (Digest::MD5.hexdigest(fragment_filename) + "_#{index}.avi")

      unless fragment_tmpfile.exist?
        ffmpeg.cut({
                     :input_video => fragment_filename,
                     :output_video => fragment_tmpfile,
                     :start => fragment.start,
                     :duration => fragment_duration
        })
        concat_files.push fragment_tmpfile
      end

      position += fragment_duration
    end

    Scissor.mencoder.concat({
                              :input_videos => concat_files,
                              :output_video => tmpfile
    })

    ffmpeg.encode({
                    :input_video => tmpfile,
                    :output_video => filename
    })
  ensure
    ffmpeg.cleanup unless options[:save_workfiles]
  end

  self.class.new(filename)
end