-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
197 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ | |
/*.mp3 | ||
|
||
/test/coverage | ||
|
||
# Ignore show name mapping | ||
/config/show_names.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,10 @@ def codec | |
name.demodulize.underscore | ||
end | ||
|
||
def lossless? | ||
bitrates.size == 1 | ||
end | ||
|
||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Convert | ||
|
||
# rubocop:disable Lint/RescueException | ||
# make sure we get notified in absolutely all cases. | ||
|
||
def self.run(directory) | ||
Converter.new(directory).run | ||
rescue Exception => e | ||
Rails.logger.error("FATAL #{e}\n #{e.backtrace.join("\n ")}") | ||
ExceptionNotifier.notify_exception(e) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module Convert | ||
class Converter | ||
|
||
SHOW_NAME_YAML = Rails.root.join('config', 'show_names.yml') | ||
|
||
class_attribute :recording_class | ||
|
||
attr_reader :directory | ||
|
||
def initialize(directory) | ||
@directory = directory | ||
end | ||
|
||
def run # rubocop:disable Metrics/MethodLength | ||
recordings = [] | ||
audio_files do |file| | ||
current = recording_class.new(file) | ||
if recordings.blank? || recordings.last.sequel?(current) | ||
recordings << current | ||
else | ||
convert(recordings) | ||
recordings = [current] | ||
end | ||
end | ||
convert(recordings) | ||
end | ||
|
||
private | ||
|
||
def audio_files(&block) | ||
Dir.glob(File.join(directory, "*.#{recording_class.extension}"), &block) | ||
end | ||
|
||
def convert(recordings) | ||
return if recordings.blank? | ||
|
||
limited = !recordings.first.audio_encoding.lossless? | ||
Import::Importer.new(build_mapping(recordings), limited_master: limited).run | ||
end | ||
|
||
def build_mapping(recordings) | ||
Import::BroadcastMapping.new.tap do |mapping| | ||
show_name = fetch_show_name(recordings.first) | ||
mapping.assign_show(name: show_name) | ||
mapping.assign_broadcast(label: show_name, | ||
started_at: recordings.first.started_at, | ||
finished_at: recordings.last.finished_at) | ||
recordings.each { |r| mapping.add_recording_if_overlapping(r) } | ||
end | ||
end | ||
|
||
def fetch_show_name(recording) | ||
show_name_mapping[recording.show_name] || recording.show_name.titleize | ||
end | ||
|
||
def show_name_mapping | ||
@show_name_mapping ||= | ||
if File.exist?(SHOW_NAME_YAML) | ||
YAML.load(File.read(SHOW_NAME_YAML)) | ||
else | ||
{} | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Convert | ||
class Mp3Recording < Recording | ||
|
||
self.extension = 'mp3' | ||
|
||
def init_values | ||
# mp3rec-05-5-20170203-080000-3600-sec-der_morgen.wav.mp3 | ||
parts = File.basename(path).match(/\-(\d{8})\-(\d{4})\d{2}\-(\d{4})-sec-(.+)\.\w+.mp3$/) | ||
@started_at = Time.zone.parse(parts[1] + ' ' + parts[2]) | ||
@duration = parts[3].to_i.seconds | ||
@show_name = parts[4] | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Convert | ||
class Recording < Import::Recording | ||
|
||
class_attribute :extension | ||
|
||
attr_reader :show_name, :started_at, :duration | ||
|
||
def initialize(path) | ||
super(path) | ||
init_values | ||
end | ||
|
||
def sequel?(other) | ||
show_name == other.show_name && | ||
other.started_at - finished_at < DURATION_TOLERANCE.seconds | ||
end | ||
|
||
def mark_imported | ||
# no-op | ||
end | ||
|
||
def audio_encoding | ||
AudioEncoding.for_extension(extension) | ||
end | ||
|
||
private | ||
|
||
def init_values | ||
# set @started_at, @duration and @show_name in subclass | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Converts a directory of audio files into the archive. | ||
|
||
require File.expand_path('../../config/environment', __FILE__) | ||
|
||
Convert.run(ARGV[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Define default classes. | ||
AudioProcessor.klass ||= AudioProcessor::Ffmpeg | ||
Import::BroadcastMapping::Builder.klass ||= Import::BroadcastMapping::Builder::AirtimeDb | ||
Convert::Converter.recording_class ||= Convert::Mp3Recording |