2019-05-09 23:26:39 +08:00
|
|
|
#!/usr/bin/ruby
|
2019-05-31 03:59:13 +08:00
|
|
|
# frozen_string_literal: true
|
2019-05-09 23:26:39 +08:00
|
|
|
|
|
|
|
# Copyright © 2019 BigBlueButton Inc. and by respective authors.
|
|
|
|
#
|
2019-05-31 03:59:13 +08:00
|
|
|
# This file is part of the BigBlueButton open source conferencing system.
|
2019-05-09 23:26:39 +08:00
|
|
|
#
|
|
|
|
# BigBlueButton is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
# Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
# option) any later version.
|
|
|
|
#
|
|
|
|
# BigBlueButton is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
2019-05-31 03:59:13 +08:00
|
|
|
# along with BigBlueButton. If not, see <https://www.gnu.org/licenses/>.
|
2019-05-09 23:26:39 +08:00
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
require 'rubygems'
|
|
|
|
require 'bundler/setup'
|
2019-05-11 02:06:23 +08:00
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
require File.expand_path('../lib/recordandplayback', __dir__)
|
2019-05-11 02:06:23 +08:00
|
|
|
|
2019-06-05 00:18:46 +08:00
|
|
|
require 'journald/logger'
|
2019-05-31 03:59:13 +08:00
|
|
|
require 'locale'
|
|
|
|
require 'rb-inotify'
|
|
|
|
require 'yaml'
|
2019-05-09 23:26:39 +08:00
|
|
|
|
2019-05-11 02:06:23 +08:00
|
|
|
# Read configuration and set up logger
|
2019-05-09 23:26:39 +08:00
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
props = File.open(File.expand_path('bigbluebutton.yml', __dir__)) do |bbb_yml|
|
|
|
|
YAML.safe_load(bbb_yml)
|
|
|
|
end
|
2019-05-09 23:26:39 +08:00
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
logger = Journald::Logger.new('bbb-rap-caption-inbox')
|
2019-05-11 02:06:23 +08:00
|
|
|
BigBlueButton.logger = logger
|
2019-05-09 23:26:39 +08:00
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
captions_dir = props['captions_dir']
|
2019-05-09 23:26:39 +08:00
|
|
|
unless captions_dir
|
2019-05-31 03:59:13 +08:00
|
|
|
logger.error('captions_dir was not defined in bigbluebutton.yml')
|
2019-05-09 23:26:39 +08:00
|
|
|
exit(1)
|
|
|
|
end
|
2019-05-31 03:59:13 +08:00
|
|
|
captions_inbox_dir = File.join(captions_dir, 'inbox')
|
2019-05-09 23:26:39 +08:00
|
|
|
|
2019-05-11 02:06:23 +08:00
|
|
|
# Internal error classes
|
|
|
|
|
|
|
|
# Base class for internal errors
|
|
|
|
class CaptionError < StandardError
|
|
|
|
end
|
|
|
|
|
|
|
|
# Indicates that uploaded caption files are invalid (unrecoverable)
|
|
|
|
class InvalidCaptionError < CaptionError
|
|
|
|
end
|
|
|
|
|
|
|
|
# Implementation
|
|
|
|
|
2019-06-05 02:01:51 +08:00
|
|
|
caption_file_notify = proc do |json_filename|
|
2019-05-11 02:06:23 +08:00
|
|
|
# There's a possible race condition where we can be notified twice for a new
|
|
|
|
# file. That's fine, just do nothing the second time.
|
|
|
|
return unless File.exist?(json_filename)
|
|
|
|
|
|
|
|
logger.info("Found new caption index file #{json_filename}")
|
|
|
|
|
|
|
|
# TODO: Rather than do anything directly in this script, it should create a
|
|
|
|
# queue job (resque?) that does the actual work.
|
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
captions_work_base = File.join(props['recording_dir'], 'caption', 'inbox')
|
2019-06-05 02:01:51 +08:00
|
|
|
new_caption_info = File.open(json_filename) { |file| JSON.parse(file.read) }
|
2019-06-04 22:50:10 +08:00
|
|
|
record_id = new_caption_info['record_id']
|
|
|
|
logger.tag(record_id: record_id) do
|
2019-06-04 02:25:13 +08:00
|
|
|
begin
|
2019-06-04 22:50:10 +08:00
|
|
|
# Read the existing captions index file
|
2019-06-04 02:25:13 +08:00
|
|
|
# TODO: This is racy if multiple tools are editing the captions.json file
|
2019-06-04 22:50:10 +08:00
|
|
|
index_filename = File.join(captions_dir, record_id, 'captions.json')
|
2019-06-04 02:25:13 +08:00
|
|
|
captions_info =
|
|
|
|
begin
|
2019-06-05 02:01:51 +08:00
|
|
|
File.open(index_filename) { |file| JSON.parse(file.read) }
|
2019-06-04 02:25:13 +08:00
|
|
|
rescue StandardError
|
|
|
|
# No captions file or cannot be read, assume none present
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
|
|
|
langtag = Locale::Tag::Rfc.parse(new_caption_info['lang'])
|
|
|
|
raise InvalidCaptionError, 'Language tag is not well-formed' unless langtag
|
|
|
|
|
2019-06-04 22:50:10 +08:00
|
|
|
# Remove the info for an existing matching track, and add the new one
|
2019-06-04 02:25:13 +08:00
|
|
|
captions_info.delete_if do |caption_info|
|
|
|
|
caption_info['lang'] == new_caption_info['lang'] &&
|
|
|
|
caption_info['kind'] == new_caption_info['kind']
|
|
|
|
end
|
|
|
|
captions_info << {
|
|
|
|
'kind' => new_caption_info['kind'],
|
|
|
|
'label' => new_caption_info['label'],
|
|
|
|
'lang' => langtag.to_s,
|
|
|
|
'source' => 'upload',
|
|
|
|
}
|
|
|
|
|
2019-06-04 22:50:10 +08:00
|
|
|
captions_work = File.join(captions_work_base, record_id)
|
|
|
|
FileUtils.mkdir_p(captions_work)
|
2019-06-04 02:25:13 +08:00
|
|
|
src_filename = File.join(captions_inbox_dir, new_caption_info['temp_filename'])
|
|
|
|
dest_filename = "#{captions_info['kind']}_#{captions_info['lang']}.vtt"
|
2019-06-04 22:50:10 +08:00
|
|
|
tmp_dest = File.join(captions_work, dest_filename)
|
|
|
|
final_dest = File.join(captions_dir, record_id, dest_filename)
|
2019-06-04 02:25:13 +08:00
|
|
|
|
2019-06-04 22:50:10 +08:00
|
|
|
# Convert the received caption file to WebVTT
|
2019-06-04 02:25:13 +08:00
|
|
|
ffmpeg_cmd = [
|
|
|
|
'ffmpeg',
|
|
|
|
'-i', src_filename, '-map', '0:s',
|
|
|
|
'-f', 'webvtt', tmp_dest,
|
|
|
|
]
|
|
|
|
ret = BigBlueButton.exec_ret(*ffmpeg_cmd)
|
|
|
|
raise InvalidCaptionError, 'FFmpeg could not read input' unless ret.zero?
|
|
|
|
|
|
|
|
FileUtils.mv(tmp_dest, final_dest)
|
2019-06-04 22:50:10 +08:00
|
|
|
|
|
|
|
# Finally, save the updated index file that references the new caption
|
2019-06-04 02:25:13 +08:00
|
|
|
File.open(index_filename, 'w') do |file|
|
2019-06-05 02:01:51 +08:00
|
|
|
file.write(JSON.pretty_generate(captions_info))
|
2019-06-04 02:25:13 +08:00
|
|
|
end
|
|
|
|
|
2019-06-04 22:50:10 +08:00
|
|
|
caption_scripts = File.glob(File.expand_path('captions/*', __dir__))
|
|
|
|
caption_scripts.each do |caption_script|
|
|
|
|
next unless File.file?(caption_script) && File.executable?(caption_script)
|
|
|
|
|
|
|
|
logger.info("Running caption integration script #{caption_script}")
|
|
|
|
ret = BigBlueButton.exec_ret(caption_script, '--record-id', record_id)
|
|
|
|
logger.warn('Caption integration script failed') unless ret.zero?
|
|
|
|
end
|
2019-06-04 02:25:13 +08:00
|
|
|
|
|
|
|
logger.info('Removing files from inbox directory')
|
|
|
|
FileUtils.rm_f(src_filename) if src_filename
|
|
|
|
FileUtils.rm_f(json_filename)
|
|
|
|
rescue InvalidCaptionError => e
|
|
|
|
logger.exception(e)
|
|
|
|
|
|
|
|
logger.info('Deleting invalid files from inbox directory')
|
|
|
|
FileUtils.rm_f(src_filename) if src_filename
|
|
|
|
FileUtils.rm_f(json_filename)
|
2019-06-04 22:50:10 +08:00
|
|
|
ensure
|
|
|
|
FileUtils.rm_rf(File.join(captions_work_base, record_id))
|
2019-05-11 02:06:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-09 23:26:39 +08:00
|
|
|
logger.info("Setting up inotify watch on #{captions_inbox_dir}")
|
|
|
|
notifier = INotify::Notifier.new
|
|
|
|
notifier.watch(captions_inbox_dir, :moved_to, :create) do |event|
|
2019-05-31 03:59:13 +08:00
|
|
|
next unless event.name.end_with?('-track.json')
|
2019-05-09 23:26:39 +08:00
|
|
|
|
2019-06-05 02:01:51 +08:00
|
|
|
caption_file_notify.call(event.absolute_name)
|
2019-05-09 23:26:39 +08:00
|
|
|
end
|
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
logger.info('Checking for missed/skipped caption files')
|
|
|
|
Dir.glob(File.join(captions_inbox_dir, '*-track.json')).each do |filename|
|
2019-06-05 02:01:51 +08:00
|
|
|
caption_file_notify.call(filename)
|
2019-05-09 23:26:39 +08:00
|
|
|
end
|
|
|
|
|
2019-05-31 03:59:13 +08:00
|
|
|
logger.info('Waiting for new caption files...')
|
2019-05-09 23:26:39 +08:00
|
|
|
notifier.run
|