From 7b169dc5065ff30f503f986ffda8d04478201d94 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 31 Aug 2022 15:28:18 -0400 Subject: [PATCH] Added project rubocop config; fixed rubocop warnings --- .rubocop.yml | 11 +++++++++++ Gemfile | 3 +++ Gemfile.lock | 23 +++++++++++++++++++++++ src/config.rb | 8 +++++--- src/repository.rb | 6 ++++-- src/transcoder.rb | 42 ++++++++++++++++++++++-------------------- src/util.rb | 2 ++ src/zealot.rb | 10 ++++++---- 8 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..cd1059d --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,11 @@ +Layout/EmptyLinesAroundClassBody: + EnforcedStyle: empty_lines + +Layout/MultilineOperationIndentation: + EnforcedStyle: indented + +Style/BlockDelimiters: + EnforcedStyle: always_braces + +Style/RedundantReturn: + Enabled: false diff --git a/Gemfile b/Gemfile index 57ef136..c7436f8 100644 --- a/Gemfile +++ b/Gemfile @@ -3,3 +3,6 @@ source "https://rubygems.org" gem "toml", "~> 0.3.0" + +gem "rubocop", "~> 1.35.1", require: false + diff --git a/Gemfile.lock b/Gemfile.lock index 577f343..c5f6563 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,37 @@ GEM remote: https://rubygems.org/ specs: + ast (2.4.2) + json (2.6.2) + parallel (1.22.1) + parser (3.1.2.1) + ast (~> 2.4.1) parslet (2.0.0) + rainbow (3.1.1) + regexp_parser (2.5.0) + rexml (3.2.5) + rubocop (1.35.1) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.1.2.1) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.20.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.21.0) + parser (>= 3.1.1.0) + ruby-progressbar (1.11.0) toml (0.3.0) parslet (>= 1.8.0, < 3.0.0) + unicode-display_width (2.2.0) PLATFORMS x86_64-linux DEPENDENCIES + rubocop (~> 1.35.1) toml (~> 0.3.0) BUNDLED WITH diff --git a/src/config.rb b/src/config.rb index 45844b5..dbe07d8 100644 --- a/src/config.rb +++ b/src/config.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'toml' # Loads and handles the application configuration. @@ -6,10 +8,10 @@ class Config # class constructor def initialize(config_path) expanded_path = File.expand_path(config_path) - @config = TOML::load_file(expanded_path) + @config = TOML.load_file(expanded_path) # just in case the user wants to use a tilde (~) in the repository path... - @config['transcoder']['repository'] = File.expand_path(self.get('transcoder.repository')) + @config['transcoder']['repository'] = File.expand_path(get('transcoder.repository')) end # returns a configuration value from a dot-seperated string, like 'transcoder.interval' @@ -17,7 +19,7 @@ class Config value = @config bits = path.split('.') bits.each { |bit| - if (value.key?(bit)) + if value.key?(bit) value = value[bit] else abort("Configuration value #{path} does exist.") diff --git a/src/repository.rb b/src/repository.rb index 1e97e20..28456fb 100644 --- a/src/repository.rb +++ b/src/repository.rb @@ -1,4 +1,6 @@ -require_relative 'util.rb' +# frozen_string_literal: true + +require_relative 'util' require 'fileutils' @@ -12,7 +14,7 @@ class Repository create_directory(@base_path) # create sub-directories - sub_paths = ['ingest', 'archive', 'output'] + sub_paths = %w[ingest archive output] sub_paths.each { |p| create_directory(File.join(@base_path, p)) } diff --git a/src/transcoder.rb b/src/transcoder.rb index cd08d2c..7c53fe5 100644 --- a/src/transcoder.rb +++ b/src/transcoder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'pathname' # Handles the actual transcoding. @@ -12,43 +14,43 @@ class Transcoder puts 'Starting transcoder...' # search for files in ingest - ingest_files = @repository.search_ingest() + ingest_files = @repository.search_ingest ingest_files.each { |ifile| # archive the file - #@repository.archive_file(ifile) + @repository.archive_file(ifile) # perform the transcode - self.transcode(ifile) + transcode(ifile) # clean up the file from ingest - #@repository.cleanup_file(ifile) + @repository.cleanup_file(ifile) } end def transcode(ingest_file) # create the ingest and output file paths ingest_path = File.join(@config.get('transcoder.repository'), 'ingest', ingest_file) - out_name = File.basename(ingest_file, File.extname(ingest_file)) + '.' + @config.get('transcoder.video_format') + out_name = "#{File.basename(ingest_file, File.extname(ingest_file))}.#{@config.get('transcoder.video_format')}" output_path = File.join(@config.get('transcoder.repository'), 'output', out_name) - # put together the ffmpeg command - ffmpeg_cmd = '/usr/bin/ffmpeg' + - " -i #{ingest_path}" + - ' -y' + - " -f #{@config.get('transcoder.video_format')}" + - " -c:v #{@config.get('transcoder.video_codec')}" + - " -s #{@config.get('transcoder.video_resolution')}" + - " -r #{@config.get('transcoder.video_framerate')}" + - " -vf 'format=#{@config.get('transcoder.video_color')}'" + - " -profile:v #{@config.get('transcoder.video_profile')}" + - " -c:a #{@config.get('transcoder.audio_codec')}" + - " #{output_path}" + ffmpeg_cmd = build_ffmpeg_cmd(ingest_path, output_path) # execute the FFMPEG command puts "Transcoding #{ingest_path} to #{output_path}." result = `#{ffmpeg_cmd} 2>&1 >/dev/null` - if result.end_with?("Conversion failed!\n") - abort "Transcoding failed with error:\n#{result}" - end + abort "Transcoding failed with error:\n#{result}" if result.end_with?("Conversion failed!\n") + end + + def build_ffmpeg_cmd(ingest_path, output_path) + # put together the ffmpeg command + "/usr/bin/ffmpeg -i #{ingest_path} -y" \ + " -f #{@config.get('transcoder.video_format')}" \ + " -c:v #{@config.get('transcoder.video_codec')}" \ + " -s #{@config.get('transcoder.video_resolution')}" \ + " -r #{@config.get('transcoder.video_framerate')}" \ + " -vf 'format=#{@config.get('transcoder.video_color')}'" \ + " -profile:v #{@config.get('transcoder.video_profile')}" \ + " -c:a #{@config.get('transcoder.audio_codec')}" \ + " #{output_path}" end end diff --git a/src/util.rb b/src/util.rb index 18ad104..46e75a1 100644 --- a/src/util.rb +++ b/src/util.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + def create_directory(path) if Dir.exist?(path) puts "Directory #{path} already exists." diff --git a/src/zealot.rb b/src/zealot.rb index f5d1b63..fdada7a 100755 --- a/src/zealot.rb +++ b/src/zealot.rb @@ -1,8 +1,10 @@ #!/usr/bin/env ruby -require_relative 'config.rb' -require_relative 'repository.rb' -require_relative 'transcoder.rb' +# frozen_string_literal: true + +require_relative 'config' +require_relative 'repository' +require_relative 'transcoder' # create new configuration instance c = Config.new('~/.config/zealot.toml') @@ -14,4 +16,4 @@ r = Repository.new(c.get('transcoder.repository')) t = Transcoder.new(c, r) # start the transcoder -t.start() +t.start