我在我的 Rails 应用程序中使用带有雾存储的 Carrierwave,并且我正在使用 Carrierwave-Video 和 Carrierwave-Video-Thumbnailer gem 来启用视频上传和从上传的视频创建缩略图。
当我上传一个 mp4 文件时,我希望它能够将其转码为 mp4,创建一个 webm 版本,并创建两个不同大小的缩略图 (.png)。现在,所有文件都已成功上传到 Amazon S3,但所有文件的内容类型都被处理为视频/快速时间。这给我带来了一个问题,因为我想在 Firefox 中使用 HTML 5 Video 标签播放 webm 文件,但由于视频的内容类型设置为 video/quicktime 而不是 video/webm,因此无法播放它们。
这是我的上传文件video_path_uploader.rb
require 'carrierwave/processing/mime_types'
class VideoPathUploader < CarrierWave::Uploader::Base
include CarrierWave::Video
include CarrierWave::Video::Thumbnailer
include CarrierWave::MimeTypes
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# 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:
def store_dir
"#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process encode_video: [:mp4]
process :set_content_type
def filename
result = [original_filename.gsub(/\.\w+$/, ""), 'mp4'].join('.') if original_filename
result
end
version :webm do
process :encode_video => [:webm]
process :set_content_type
def full_filename(for_file)
"#{File.basename(for_file, File.extname(for_file))}.webm"
end
end
version :thumb do
process thumbnail: [{format: 'png', quality: 10, size: 158, logger: Rails.logger}]
def full_filename for_file
png_name for_file, version_name
end
process :set_content_type
# process resize_to_limit: [105, 158]
end
version :square_thumb do
process thumbnail: [{format: 'png', quality: 10, size: 105, strip: false, logger: Rails.logger}]
def full_filename for_file
png_name for_file, version_name
end
process :set_content_type
# process resize_to_fill: [105, 105]
end
def png_name for_file, version_name
%Q{#{version_name}_#{for_file.chomp(File.extname(for_file))}.png}
end
end
我上传视频时的 Rails 日志:
Running transcoding...
ffmpeg -y -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/Untitled.mov -vcodec libx264 -acodec libfaac -s 640x360 -qscale 0 -preset slow -g 30 -aspect 1.7777777777777777 /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.mp4
Transcoding of /Documents/Website/public/uploads/tmp/1382543537-3350-3237/Untitled.mov to /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.mp4 succeeded
Running transcoding...
ffmpeg -y -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/webm_Untitled.mov -vcodec libvpx -acodec libvorbis -s 640x360 -b 1500k -ab 160000 -f webm -g 30 -aspect 1.7777777777777777 /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.webm
Transcoding of /Documents/Website/public/uploads/tmp/1382543537-3350-3237/webm_Untitled.mov to /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.webm succeeded
Invalid Excon request keys: :scheme, :host
/.rvm/gems/ruby-1.9.3-p448/gems/excon-0.27.6/lib/excon/connection.rb:232:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/connection.rb:57:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/deprecated/connection.rb:20:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/aws/storage.rb:513:in `request'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/aws/requests/storage/put_object.rb:40:in `put_object'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/aws/models/storage/file.rb:211:in `save'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/collection.rb:52:in `create'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:260:in `store'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:80:in `store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:59:in `block in store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:58:in `store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:371:in `store!'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:223:in `store_video_path!'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:460:in `_run__3293958342697848958__save__4139166616116124146__callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:405:in `__run_callback'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/activesupport-3.2.15/lib/active_support/callbacks.rb:81:in `run_callbacks'
/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-...
Started POST "/videos" for 127.0.0.1 at 2013-10-23 11:52:17 -0400
Processing by VideosController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jv5yj+KWLjlCOpBmTkpypdHjgZtTlc+s1cA973b1MF4=", "video"=>{"project_id"=>"10", "step_id"=>"97", "saved"=>"true", "embed_url"=>"", "video_path"=>#<ActionDispatch::Http::UploadedFile:0x007fecd6120e38 @original_filename="Untitled.mov", @content_type="video/quicktime", @headers="Content-Disposition: form-data; name=\"video[video_path]\"; filename=\"Untitled.mov\"\r\nContent-Type: video/quicktime\r\n", @tempfile=#<File:/var/folders/dc/c0nfvwy96lq7p4ll94mklnmr0000gp/T/RackMultipart20131023-3350-2gln6f>>}}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Project Load (0.4ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT 1 [["id", "10"]]
Running....ffmpegthumbnailer -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/thumb_Untitled.mov -o /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.png -c png -q 10 -s 158
Success!
Running....ffmpegthumbnailer -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/square_thumb_Untitled.mov -o /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.png -c png -q 10 -s 105
Success!
(0.2ms) BEGIN
SQL (53.5ms) INSERT INTO "videos" ("created_at", "embed_url", "image_id", "project_id", "saved", "step_id", "thumbnail_url", "updated_at", "video_path") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id" [["created_at", Wed, 23 Oct 2013 11:52:25 EDT -04:00], ["embed_url", ""], ["image_id", nil], ["project_id", 10], ["saved", true], ["step_id", 97], ["thumbnail_url", nil], ["updated_at", Wed, 23 Oct 2013 11:52:25 EDT -04:00], ["video_path", "Untitled.mp4"]]
(0.6ms) COMMIT
(0.3ms) SELECT COUNT(*) FROM "images" WHERE "images"."project_id" = 10 AND "images"."step_id" = 97
(0.1ms) BEGIN
SQL (5.4ms) INSERT INTO "images" ("caption", "created_at", "image_path", "original_id", "position", "project_id", "saved", "sound_id", "step_id", "updated_at", "video_id", "video_thumbnail") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id" [["caption", nil], ["created_at", Wed, 23 Oct 2013 11:52:29 EDT -04:00], ["image_path", "thumb_Untitled.png"], ["original_id", nil], ["position", 0], ["project_id", 10], ["saved", true], ["sound_id", nil], ["step_id", 97], ["updated_at", Wed, 23 Oct 2013 11:52:29 EDT -04:00], ["video_id", 158], ["video_thumbnail", nil]]
(0.5ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
(0.1ms) BEGIN
(0.1ms) COMMIT
(0.1ms) BEGIN
(0.4ms) UPDATE "videos" SET "image_id" = 348, "updated_at" = '2013-10-23 15:52:30.679474' WHERE "videos"."id" = 158
(0.4ms) COMMIT
Project Load (0.3ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = 10 LIMIT 1
SQL (0.7ms) UPDATE "projects" SET "updated_at" = '2013-10-23 15:52:30.684551' WHERE "projects"."id" = 10
Step Load (0.4ms) SELECT "steps".* FROM "steps" WHERE "steps"."id" = 97 LIMIT 1
Image Load (0.2ms) SELECT "images".* FROM "images" WHERE "images"."step_id" = 97
Video Load (0.2ms) SELECT "videos".* FROM "videos" WHERE "videos"."image_id" = 348 LIMIT 1
Rendered images/_edit_image.html.erb (3.8ms)
Rendered images/create.js.erb (12.3ms)
Completed 200 OK in 13069.3ms (Views: 15.3ms | ActiveRecord: 64.6ms)
这就是在我的页面上呈现的内容:
<video controls>
<source src="https://...s3.amazonaws.com/video/video_path/158/Untitled.webm" class="348" id="video_348" type="video/webm">
<source src="https://...s3.amazonaws.com/video/video_path/158/Untitled.mp4" class="348" id="video_348" type="video/mp4">
</video>