0

내 Rails 앱에서 안개가있는 Carrierwave를 사용하고 있으며 Carrierwave-Video 및 Carrierwave-Video-Thumbnailer 보석을 사용하여 업로드 한 동영상에서 동영상 업로드 및 미리보기 이미지 만들기를 사용하고 있습니다.반송파 설정 content_type이 잘못되었습니다

mp4 파일을 업로드 할 때 mp4 파일로 변환하여 mp4로 만들고 webm 버전을 만들고 두 가지 다른 크기의 미리보기 이미지 (.png)를 만들 것을 기대합니다. 현재 모든 파일이 Amazon S3에 성공적으로 업로드되지만 컨텐츠 유형은 모든 파일에 대해 비디오/퀵타임으로 처리됩니다. 이것은 HTML 5 Video 태그를 사용하여 Firefox에서 webm 파일을 재생하려고하기 때문에 문제가되는데 비디오의 콘텐츠 유형이 video/webm이 아닌 video/quicktime으로 설정되어 있기 때문에 재생할 수 없습니다.

이 내 업 로더 파일, 나는 비디오가 업로드 될 때

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 

내 레일 로그 video_path_uploader.rb :

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> 

답변

4

수동으로 콘텐츠 유형을 설정하는 방법을 찾았 으면 내 대답을 업데이트하는 것을 잊어 버렸습니다. 이것이 내가 끝내었던 것입니다.

require 'carrierwave/processing/mime_types' 
class VideoPathUploader < CarrierWave::Uploader::Base 
    include CarrierWave::MimeTypes 
    process :encode 

    def encode 
    video = MiniExiftool.new(@file.path) 
    orientation = video.rotation 
    model.rotation = orientation 
    Rails.logger.debug("orientation: #{orientation}") 
    Rails.logger.debug("video wxh #{video.imagewidth} #{video.imageheight}") 

    if orientation == 90 && video.imagewidth.to_f > video.imageheight.to_f 
     Rails.logger.debug("rotating video") 
     aspect_ratio = video.imageheight.to_f/video.imagewidth.to_f 
     encode_video(:mp4, custom: "-vf transpose=1", resolution: :same, aspect: aspect_ratio) 
    else 
     aspect_ratio = video.imagewidth.to_f/video.imageheight.to_f 
     encode_video(:mp4, resolution: :same, aspect: aspect_ratio) 
    end 

    instance_variable_set(:@content_type, "video/mp4") 
    :set_content_type_mp4 
    end 

    version :webm do 
    process :encode_video => [:webm] 
    process :set_content_type_webm 
    def full_filename(for_file) 
     "#{File.basename(for_file, File.extname(for_file))}.webm" 
    end 
    end 

def set_content_type_mp4(*args) 
    Rails.logger.debug "#{file.content_type}" 
    self.file.instance_variable_set(:@content_type, "video/mp4") 
    end 

    def set_content_type_webm(*args) 
    Rails.logger.debug "#{file.content_type}" 
    self.file.instance_variable_set(:@content_type, "video/webm") 
    end 
end 

희망이 있습니다.

+0

나를 도왔습니다. 감사! – moritz

0

야심을 느끼는 경우,이 버전에서 작동하지 못하도록 캐리어 웨이브 자체의 버그로 격리 할 수 ​​있습니다.

빠른 검토에서 은 full_filename 대신에 the base file's original_filename을 사용합니다. that'sset_content_type은 s3으로 전송 된 콘텐츠 유형을 조회하는 데 사용됩니다.