You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于CarrierWave视频转码前后设置视频长度限制的咨询

Adding Video Duration Limits with CarrierWave (Before/After Transcoding)

Great question! Even though CarrierWave's official docs don't explicitly cover video duration limits, you absolutely can implement checks both before and after transcoding using its built-in validation hooks and the after_transcode callback you mentioned. Let's walk through the steps:

Before Transcoding (Validate on Upload)

Before your video even starts transcoding, you can add a custom validation to check the original file's duration. This saves you from wasting resources on transcoding videos that are already too long.

First, make sure your system has ffprobe (part of the FFmpeg suite) installed—this is how we'll extract the video duration. Then, add this validation to your CarrierWave uploader:

class VideoUploader < CarrierWave::Uploader::Base
  include CarrierWave::Video
  include CarrierWave::Video::Thumbnailer

  # Your existing transcoding config here
  process encode_video: [:mp4, resolution: "720p"]
  after_transcode :set_success

  # Custom validation for pre-transcode duration check
  validate :validate_video_duration_before_transcode

  private

  def validate_video_duration_before_transcode
    return unless file.present? && file.content_type.start_with?('video/')

    # Use ffprobe to get the video duration in seconds
    duration_output = `ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 #{file.path}`
    duration = duration_output.to_f

    # Set your max allowed duration (e.g., 300 seconds = 5 minutes)
    max_duration = 300

    if duration > max_duration
      errors.add(:file, "Video duration cannot exceed #{max_duration/60} minutes")
    end
  end

  def set_success
    # Your existing set_success logic here (e.g., mark record as processed)
    model.video_processed = true
  end
end

This validation runs when the file is uploaded, before any transcoding starts. If the video is too long, it adds an error and prevents the upload from proceeding.

After Transcoding (Post-Processing Check)

If you want to double-check the duration after transcoding (for example, to ensure the transcoding process didn't accidentally alter the length, or if you're applying cuts during transcoding), you can add an additional callback tied to after_transcode:

class VideoUploader < CarrierWave::Uploader::Base
  # ... existing code ...

  after_transcode :validate_video_duration_after_transcode

  private

  def validate_video_duration_after_transcode
    # Get the path to the transcoded file (adjust based on your output format)
    transcoded_file_path = File.join(current_path, '..', "#{model.video.file.filename.base}.mp4")

    return unless File.exist?(transcoded_file_path)

    duration_output = `ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 #{transcoded_file_path}`
    duration = duration_output.to_f
    max_duration = 300

    if duration > max_duration
      # Handle the error—you could delete the transcoded file, mark the record as failed, etc.
      File.delete(transcoded_file_path) if File.exist?(transcoded_file_path)
      errors.add(:file, "Transcoded video exceeds the maximum allowed duration of #{max_duration/60} minutes")
      model.video_processed = false
    end
  end

  # ... existing set_success method ...
end

A few quick notes:

  • Adjust the transcoded_file_path to match how your transcoding setup names and stores output files.
  • If the post-transcode check fails, you'll want to clean up the invalid file and update your model's state accordingly.

Key Takeaways

  • Pre-transcode validation is the most efficient way to block oversized videos early, saving processing resources.
  • Post-transcode checks are useful for verifying transcoding results or enforcing limits if you're modifying the video length during processing.
  • Both approaches rely on FFmpeg's ffprobe to read video metadata—make sure it's installed on your server/development environment.

内容的提问来源于stack exchange,提问作者user9157191

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:24:45