Skip to content
Snippets Groups Projects
Commit 5d235cf4 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

add video converter worker and task

parent 94405aab
No related branches found
No related tags found
No related merge requests found
class ConvertVideoWorker
include Sidekiq::Worker
include RepositoriesProxy
sidekiq_options queue: :convert_video
def perform(id)
object = learning_object_repository.find @learning_object.id
# convert if object has a flv and not a mp4
flv = nil
object.attachments.each do |attachment|
flv = attachment if attachment.name =~ /.flv$/i
return true if attachment.name =~ /.mp4$/i
end
return true if flv.nil?
dspace_client = DspaceService.create_client
# retrieve video from dspace
video = dspace_client.bitstreams.retrieve(id: flv.id)
# open video with ffmpeg
# FFMPEG.ffmpeg_binary = '/usr/local/bin/ffmpeg'
FFMPEG.ffmpeg_binary = 'ffmpeg'
file = ::FFMPEG::Movie.new(video.path)
return false unless file.valid? # if ffmpeg fails to read the movie
converted_path = "#{video.path}.mp4"
# transcode (convert video to mp4)
options = {
video_codec: "libx264",
x264_preset: "fast",
custom: '-movflags +faststart -pix_fmt yuv420p -profile:v main -level 3.1 -refs 4 -c:a libfdk_aac',
validate: true
}
file.transcode(converted_path, options)
converted = File.open converted_path
# send converted video to dspace
dspace_client.items.add_bitstream(converted, id: object.id_dspace, name: "#{flv.name.split('.')[0..-2].join('.')}.mp4", description: 'converted from original flv')
# remove temp files
video.unlink
converted.close
FileUtils.rm converted_path, force: true
end
end
......@@ -10,6 +10,7 @@
:queues:
- default
- score
- convert_video
- [high_priority, 2]
# :daemon: true
......
namespace :score do
desc "Generate Score"
task :learning_object => :environment do
include RepositoriesProxy
# Quantity of items fetched on each iteration
limit = 500
# Start point from where items will be fetched
offset = 0
loop do
begin
# Get items from dspace (from offset to offset+limit)
items = learning_object_repository.all_from_offset_to_limit(offset,limit)
rescue
# Sleeps for a while to wait database's recovery
sleep(10.seconds)
# Goes to next iteration to retry
next
else
# Terminate loop if there are no more items to import
break if items.empty?
items.each do |item|
item.attachments.each do |attachment|
ConvertVideoWorker.perform_async(item.id) if attachment.name =~ /.flv$/i
end
end
# Increment offset, to get new items on next iteration
offset += limit
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment