I am using Ffmpeg to record video in a rails app. I want to give the user a minimum and maximum time limit. What is the best flag to use to set this restraint?
2 Answers
In ffmpeg
use the -t
(or -to
) option to set the duration. Example for 120 seconds:
-t 120
or
-t 00:02:00
See FFmpeg Documentation.

- 121,796
- 28
- 232
- 243
That's a bit beyond the scope of the basic validation that Paperclip does. Paperclip tries to gives you enough validations to cover 95% of your use cases, but for anything that's just a bit less than typical, you'll have to write your own logic. I don't believe there is a custom validation built-in for checking the length of videos. (However, there is a validates_attachment_size
validation method you can use for validating the filesize of the asset. In a pinch, this might suffice.)
If you want to do some deeper validations on paperclip attachments, you should look into some different tools to write your own validation methods with. I'd take a look at streamio-ffmpeg if you're dealing with video.
Using that gem you could write a custom validation something like this:
class YourModel < ActiveRecord::Base
has_attached_file :your_media
validate :duration_in_range
private
def duration_in_range
# See https://github.com/streamio/streamio-ffmpeg#usage
unless FFMPEG::Movie.new(your_media.path).duration.in?(MIN_TIME..MAX_TIME)
errors.add(:your_media, "Video duration must be within #{MIN_TIME} and #{MAX_TIME} seconds")
end
end
end

- 2,071
- 1
- 14
- 28