Skip to content

Instantly share code, notes, and snippets.

@colewinans
Last active August 29, 2015 13:57
Show Gist options
  • Save colewinans/9486307 to your computer and use it in GitHub Desktop.
Save colewinans/9486307 to your computer and use it in GitHub Desktop.
class VideoWorker
include Sidekiq::Worker
def perform(id, s3_key)
# Get the video
video = Video.find(id)
# Call ffmpeg to measure duration
duration = `ffmpeg -i '#{video.ffmpeg_url}' 2>&1 | grep Duration | awk '{print $2}' | tr -d ,`
# Parse duration timestamp into seconds
t = Time.parse(duration)
sec = t.hour*3600 + t.min*60 + t.sec
# If video duration <= 30s, encode it
if sec <= 60
zencoder_response = Zencoder::Job.create({
:input => video.s3_key,
:test => true,
:notifications => "[]",
:pass_through => video.id,
:outputs => [
{
:credentials => "[]",
:public => true,
:base_url => video.base_url,
:label => 'webmp4',
:format => 'mp4',
:audio_codec => 'aac',
:video_codec => 'h264',
:size => '440x440',
:upscale => true,
:aspect_mode => "crop"
},
{
:credentials => "[]",
:public => true,
:base_url => video.base_url,
:label => 'webwebm',
:format => 'webm',
:audio_codec => 'vorbis',
:video_codec => 'vp8',
:size => '440x440',
:upscale => true,
:aspect_mode => "crop"
},
{
:thumbnails => {
:credentials => "[]",
:public => true,
:base_url => video.base_url,
:times => [1],
:size => '440x440',
:aspect_mode => "crop"
}
}
]
})
else
# If video duration is > 30s, destroy it
video.destroy
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment