Class: Venus::SplitVideo
- Inherits:
-
Object
- Object
- Venus::SplitVideo
- Defined in:
- lib/components/split_video.rb
Class Method Summary collapse
- .build_command(input_file, output_path, segment_duration, format_video, path, output_prefix) ⇒ Object
- .convert_duration(format_segment, segment_duration) ⇒ Object
- .execute_command(command) ⇒ Object
- .handle_format_video(format_video, output_prefix, _segment_duration, _path) ⇒ Object
- .process_arguments(args_string) ⇒ Object
- .split_video(input_file, output_prefix, format_segment, segment_duration, path: nil, format_video: nil) ⇒ Object
Class Method Details
.build_command(input_file, output_path, segment_duration, format_video, path, output_prefix) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/components/split_video.rb', line 31 def self.build_command(input_file, output_path, segment_duration, format_video, path, output_prefix) if format_video == 'first' output_path = path ? File.join(path, "#{output_prefix}_001.mp4") : "#{output_prefix}_001.mp4" "ffmpeg -i #{input_file} -c copy -map 0 -t #{segment_duration} #{output_path}" else "ffmpeg -i #{input_file} -c copy -map 0 -segment_time #{segment_duration} -f segment -reset_timestamps 1 #{output_path}" end end |
.convert_duration(format_segment, segment_duration) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/components/split_video.rb', line 17 def self.convert_duration(format_segment, segment_duration) case format_segment when 'min' segment_duration.to_i * 60 when 'hour' segment_duration.to_i * 3600 when 'sek' segment_duration.to_i else puts "Invalid format. Use 'min', 'hour' or 'sek'." nil end end |
.execute_command(command) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/components/split_video.rb', line 40 def self.execute_command(command) Open3.popen3(command) do |_stdin, _stdout, stderr, wait_thr| while (line = stderr.gets) puts line end exit_status = wait_thr.value unless exit_status.success? puts "Error executing command: #{command}" exit 1 end end end |
.handle_format_video(format_video, output_prefix, _segment_duration, _path) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/components/split_video.rb', line 54 def self.handle_format_video(format_video, output_prefix, _segment_duration, _path) parts = Dir.glob("#{output_prefix}_*.mp4").sort case format_video when 'end' File.delete(*parts[0...-1]) if parts.length > 1 else range = eval(format_video) parts.each_with_index { |part, index| File.delete(part) unless range.include?(index + 1) } end end |
.process_arguments(args_string) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/components/split_video.rb', line 65 def self.process_arguments(args_string) args = args_string.split if args.length < 4 || args.length > 6 puts 'Usage: ruby main.rb <input_file> <output_prefix> <format_segment> <segment_duration> [path] [format_video]' exit 1 end args end |
.split_video(input_file, output_prefix, format_segment, segment_duration, path: nil, format_video: nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/components/split_video.rb', line 5 def self.split_video(input_file, output_prefix, format_segment, segment_duration, path: nil, format_video: nil) segment_duration = convert_duration(format_segment, segment_duration) return unless segment_duration output_path = path ? File.join(path, "#{output_prefix}_%03d.mp4") : "#{output_prefix}_%03d.mp4" command = build_command(input_file, output_path, segment_duration, format_video, path, output_prefix) execute_command(command) handle_format_video(format_video, output_prefix, segment_duration, path) if format_video end |