在初始记录插入数据库后,我设置了 carrierwave_backgrounder , carrierwave-video 和 carrierwave-ffmpegthumbnailer 来对视频文件进行编码( PostgreSQL)。我遇到的问题是,我只能在Rails控制台中启动编码过程。出于某种奇怪的原因,当我使用表格时它并没有开始。我有sidekiq和carrierwave-backgrounder的默认配置设置。我也在处理mp4或webm中的每个文件。记录存储到数据库中,其中video列为null。它在我的服务器开发日志中未显示任何有关编码或sidekiq作业的内容。我在web ui上检查了sidekiq,也没有任何内容。到目前为止,我已完成以下操作。我正在使用Rails 5.2,顺便说一句。如何使用sidekiq和carrierwave_background成功编码视频?
carrierwave_backgrounder.rb
CarrierWave::Backgrounder.configure do |c|
c.backend :sidekiq, queue: :carrierwave
end
sidekiq.rb
require 'sidekiq'
Sidekiq.configure_client do |config|
config.redis = {:size => 1}
end
Sidekiq.configure_server do |config|
end
story_video_uploader.rb
class StoryVideoUploader < CarrierWave::Uploader::Base
include ::CarrierWave::Backgrounder::Delay
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
include CarrierWave::Video
# include CarrierWave::Video::Thumbnailer
#include CarrierWave::FFMPEG
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
=begin
version :thumb do
process thumbnail: [{ format: 'png', quality: 10, size: 200, strip: true, logger: Rails.logger }]
def full_filename(for_file)
png_name(for_file, version_name)
end
end
=end
version :mp4 do
process encode_video: [:mp4, preserve_aspect_ratio: :height,
video_codec: 'libx264', # H.264/MPEG-4 AVC video codec
constant_rate_factor: '30', # GOP Size
frame_rate: '25', # Frame rate
audio_codec: 'aac', # AAC audio codec
audio_bitrate: '64k', # Audio bitrate
audio_sample_rate: '44100' # Audio sampling frequency
]
end
version :webm do
process encode_video: [:webm, preserve_aspect_ratio: :height,
video_codec: 'libvpx', # H.264/MPEG-4 AVC video codec
constant_rate_factor: '30', # GOP Size
frame_rate: '25', # Frame rate
audio_codec: 'libvorbis', # AAC audio codec
audio_bitrate: '64k', # Audio bitrate
audio_sample_rate: '44100' # Audio sampling frequency
]
end
def png_name(for_file, version_name)
%Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.png}
end
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
story.rb
class Story < ApplicationRecord
reverse_geocoded_by :latitude, :longitude, :address => :location
include UniqueIdentifierLinksHelper
belongs_to :user, optional: true
mount_uploader :story_video, StoryVideoUploader
process_in_background :story_video
end
stories_controller.rb
class StoriesController < ApplicationController
before_action :set_story, only: %i[show edit update destroy like unlike]
before_action :authenticate_user!
# GET /stories
# GET /stories.json
def index
@stories = current_user.stories.order(created_at: :desc).page(params[:page]).per(20)
end
# GET /stories/1
# GET /stories/1.json
def show
impressionist(@story)
end
# GET /stories/new
def new
@story = Story.new
end
# GET /stories/1/edit
def edit; end
# POST /stories
# POST /stories.json
def create
@story = current_user.stories.build(story_params)
respond_to do |format|
if @story.save
format.html {redirect_to @story, notice: 'Your story was created!'}
#format.json { render :show, status: :created, location: @story }
#format.js
else
format.html {render :new}
#format.json { render json: @story.errors, status: :unprocessable_entity }
#format.js
end
end
end
# PATCH/PUT /stories/1
# PATCH/PUT /stories/1.json
def update
respond_to do |format|
if @story.update(story_params)
format.json { head :no_content }
format.js
else
format.json { render json: @story.errors, status: :unprocessable_entity }
end
end
end
# DELETE /stories/1
# DELETE /stories/1.json
def destroy
@story.destroy
respond_to do |format|
format.js
format.html { redirect_to stories_url }
format.json { head :no_content }
end
end
def like
@story.liked_by current_user
respond_to do |format|
format.html { redirect_to :back }
format.js
end
end
def unlike
@story.unliked_by current_user
respond_to do |format|
format.html { redirect_to :back }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_story
@story = Story.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def story_params
params.require(:story).permit(:story_video, :story_video_processing)
end
end