Added project rubocop config; fixed rubocop warnings

This commit is contained in:
Gregory Ballantine 2022-08-31 15:28:18 -04:00
parent 92fcfd30f2
commit 7b169dc506
8 changed files with 76 additions and 29 deletions

11
.rubocop.yml Normal file
View File

@ -0,0 +1,11 @@
Layout/EmptyLinesAroundClassBody:
EnforcedStyle: empty_lines
Layout/MultilineOperationIndentation:
EnforcedStyle: indented
Style/BlockDelimiters:
EnforcedStyle: always_braces
Style/RedundantReturn:
Enabled: false

View File

@ -3,3 +3,6 @@
source "https://rubygems.org"
gem "toml", "~> 0.3.0"
gem "rubocop", "~> 1.35.1", require: false

View File

@ -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

View File

@ -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.")

View File

@ -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))
}

View File

@ -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}"
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

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
def create_directory(path)
if Dir.exist?(path)
puts "Directory #{path} already exists."

View File

@ -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