Question

I trying to upload large file (upto 2 gb) from my sample app. I am using carrierwave gem for file uploading

While uploading files it is taking too much time and also it is allocating server process while uploading. After searching I get this link. So I tried to use delayed job for background process.

For using combine carrierwave and delayed job I followed this tutorial. I performed all steps as mentioned in the tutorial. File is uploading successfully.

But after I looked into the uploaded directory I get that file is uploading twice with different names example - bitrate_128k_IMG_5099.MOV and IMG_5099.MOV

I tried to debug it but I didn't get any solution for it.

video.rb

class Video < ActiveRecord::Base  
  mount_uploader :video, VideoUploader  
  after_save :enqueue  
end

videos_controller.rb

class VideosController < ApplicationController    
  def index
  end

  def show
    @video = Video.find(params[:id])
  end

  def new
    @video = Video.new(video_params)    
  end

  def create    
    @video = Video.new(video_params)    
    if @video.save
      redirect_to @video
    else
      render :nothing => true, :status => 400
    end
  end

  private
  def video_params
    params.require(:video).permit(:attachment, :zencoder_output_id, :processed, :video, :meta_info, :processing) if params[:video]
  end 
end

/uploader/video_uploader.rb

# encoding: utf-8
require File.join(Rails.root, "lib", "carrier_wave", "ffmpeg") 
require File.join(Rails.root, "lib", "carrier_wave", "delayed_job")

class VideoUploader < CarrierWave::Uploader::Base

  include CarrierWave::Delayed::Job 
  include CarrierWave::FFMPEG 

  storage :file

  def store_dir
    "videos/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :bitrate_128k do 
    process :resample => "128k" 
  end

  def extension_white_list
    %w(avi mov mkv mpg mpeg mp4 m4v flv ogv)
  end  
end

lib/carrier_wave/delayed_job.rb

# lib/carrier_wave/delayed_job.rb
module CarrierWave
  module Delayed
    module Job
      module ActiveRecordInterface
        def delay_carrierwave
          @delay_carrierwave ||= true
        end

        def delay_carrierwave=(delay)
          @delay_carrierwave = delay
        end

        def perform
          asset_name = self.class.uploader_options.keys.first
          self.send(asset_name).versions.each_pair do |key, value|
            value.process_without_delay!
          end
        end

        private
        def enqueue
          ::Delayed::Job.enqueue self
        end
      end

      def self.included(base)
        base.extend ClassMethods
      end

      module ClassMethods
        def self.extended(base)
          base.send(:include, InstanceMethods)
          base.alias_method_chain :process!, :delay
          ::ActiveRecord::Base.send(:include, CarrierWave::Delayed::Job::ActiveRecordInterface)
        end

        module InstanceMethods
          def process_with_delay!(new_file)
            process_without_delay!(new_file) unless model.delay_carrierwave
          end
        end
      end
    end
  end
end

controller/videos/new.html.erb

<h2>Upload your Video</h2>

<%= form_for @video, :html => {:multipart => true} do |f| %>  
  <p>
    <%= f.label :attachment %><br />
    <%= f.text_field :attachment %>
  </p>
  <p>
    <%= f.file_field :video %>
  </p>
  <p><%= f.submit %></p>
<% end %>

How can I stop this two times recording. I am using rails 4 for this.

Was it helpful?

Solution

This is because CarrierWave stores the original file too. Try changing this:

version :bitrate_128k do 
  process :resample => "128k" 
end

to:

process :resample => "128k" 

This should just store one file which is resampled to 128k.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top