carrierwave设置CONTENT_TYPE不正确 [英] carrierwave setting content_type incorrectly

查看:206
本文介绍了carrierwave设置CONTENT_TYPE不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Carrierwave雾存储在我的Rails应用程序,而我使用的Carrierwave-Video和Carrierwave视频 - thumbnailer的宝石,使视频从上传视频上传和缩略图创造。

当我上传的MP4文件,我希望它到C是跨$ C $到MP4中,创建一个WEBM版本,并创建两个不同大小的缩略图文件(.png)。现在,所有的文件都成功上传到Amazon S3,但内容类型是越来越处理视频/ QuickTime的所有文件。这是对我造成一个问题,因为我想使用HTML 5视频标签播放WEBM文件在Firefox,它是不能够因为视频的内容类型,以发挥他们被设置为视频/ QuickTime的,而不是视频/ W​​EBM。

这是我上传的文件, video_path_uploader.rb

 要求carrierwave /处理/ mime_types

类VideoPathUploader< CarrierWave ::上传::基地
  包括CarrierWave ::视频
  包括CarrierWave ::视频:: thumbnailer的
  包括CarrierWave :: MIMETYPES

  #包括RMagick或MiniMagick支持:
  #包括CarrierWave :: RMagick
  包括CarrierWave :: MiniMagick

  #选择使用此上传什么样的存储:
  #存储:文件
  存储:雾

  #覆盖的地方上传的文件将被存储在目录中。
  #这是一个合理的默认为是为了安装上传者:
  高清store_dir
    #{model.class.to_s.underscore} / {#} mounted_as /#{model.id}
  结束

  过程连接code_video:[:MP4]

  过程:set_content_type

  DEF文件名
    结果= [original_filename.gsub(/ \。\ w + $ /,),MP4。联接('。'),如果original_filename
    结果
  结束

  版本:WEBM做
    过程:恩code_video => [:WEBM]
    过程:set_content_type
    高清full_filename(for_file)
      #{File.basename(for_file,File.extname(for_file))}。WEBM
    结束
  结束


  版本:拇指做
    流程缩略图:[{格式:PNG,质量:10,尺寸:158,记录器:Rails.logger}]
    高清full_filename for_file
       png_name for_file,VERSION_NAME
    结束
    过程:set_content_type
    #过程resize_to_limit:[105,158]
  结束

  版本:square_thumb做
     流程缩略图:[{格式:PNG,质量:10,规格:105条:假的,记录器:Rails.logger}]
    高清full_filename for_file
      png_name for_file,VERSION_NAME
    结束
    过程:set_content_type
    #过程resize_to_fill:[105,105]
  结束

  DEF png_name for_file,VERSION_NAME
    %Q {#{VERSION_NAME} _#{for_file.chomp(File.extname(for_file))}。PNG}
  结束

结束
 

我的铁轨登录时,我上传了一个视频:

 运行转码...
的ffmpeg -y -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/Untitled.mov -v codeC libx264 -a codeC libfaac -s 640×360 -qscale 0  -  preSET慢-g 30 -aspect 1.7777777777777777 /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.mp4

/Documents/Website/public/uploads/tmp/1382543537-3350-3237/Untitled.mov的转码来/Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.mp4成功

运行转码...
的ffmpeg -y -i /Documents/Website/public/uploads/tmp/1382543537-3350-3237/webm_Untitled.mov -v codeC libvpx -a codeC libvorbis -s 640×360 -b 1500K -ab 160000 -f WEBM -g 30 -aspect 1.7777777777777777 /Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.webm

/Documents/Website/public/uploads/tmp/1382543537-3350-3237/webm_Untitled.mov的转码来/Documents/Website/public/uploads/tmp/1382543537-3350-3237/tmpfile.webm成功

无效EXCON请求键:方案:主机
/.rvm/gems/ruby-1.9.3-p448/gems/excon-0.27.6/lib/excon/connection.rb:232:in'要求'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/connection.rb:57:in'要求'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/de$p$pcated/connection.rb:20:in'要求'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/aws/storage.rb:513:in'要求'
/.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'救'
/.rvm/gems/ruby-1.9.3-p448/gems/fog-1.16.0/lib/fog/core/collection.rb:52:in'创建'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:260:in`商店'
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:80:in'店!
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/uploader/store.rb:59:in`挡在店!
/.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'店!
/.rvm/gems/ruby-1.9.3-p448/gems/carrierwave-0.9.0/lib/carrierwave/mount.rb:371:in'店!
/.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 -...


开始POST/视频为127.0.0.1,在2013年10月23日11时52分17秒-0400
处理由VideosController#创建为JS
  参数:{UTF8=&gt;中✓,authenticity_token=&gt;中jv5yj + KWLjlCOpBmTkpypdHjgZtTlc + s1cA973b1MF4 =,视频=&GT; {PROJECT_ID=&GT;10,step_id的数据类型=&GT ;97,保存=&gt;中真,embed_url=&gt;中,video_path=&GT;#&所述; ActionDispatch :: HTTP :: UploadedFile的:0x007fecd6120e38 @ original_filename =Untitled.mov ,@ CONTENT_TYPE =视频/ QuickTime的,@标题=内容处置:表格数据;名称= \视频[video_path] \文件名= \Untitled.mov \\ r \ nContent类型:视频/ QuickTime的\ r \ N, @tempfile=#<File:/var/folders/dc/c0nfvwy96lq7p4ll94mklnmr0000gp/T/RackMultipart20131023-3350-2gln6f>>}}
  用户负载(0.6ms)选择用户。*从用户WHERE用户,ID= 1 LIMIT 1
  项目负荷(0.4ms)选择项目。*从项目WHERE项目,ID= $ 1 LIMIT 1 [ID,10]
运行.... 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
成功!
运行.... 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
成功!
   (为0.2ms)BEGIN
  SQL(53.5ms)INSERT INTO视频(created_at,embed_url,image_id,PROJECT_ID,拯救,step_id的数据类型,thumbnail_url,的updated_at,video_path)VALUES($ 1 ,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7 $ 8 $ 9)归还ID[[created_at,星期三,2013年10月23日11点52分25秒EDT -04:00],[embed_url, ],[image_id,零],[PROJECT_ID,10],[保存,真],[step_id的数据类型,97],[thumbnail_url,零],[的updated_at,星期三, 2013年10月23日11时52分25秒EDT -04:00],[video_path,Untitled.mp4]
   (0.6ms)COMMIT
   (0.3ms)SELECT COUNT(*)FROM图像WHERE图像,PROJECT_ID= 10,图像,step_id的数据类型= 97
   (0.1毫秒)BEGIN
  SQL(5.4ms)INSERT INTO图像(标题,created_at,image_path,original_id,位置,PROJECT_ID,拯救,sound_id,step_id的数据类型,的updated_at VIDEO_ID,video_thumbnail)VALUES($ 1,$ 2,$ 3,$ 4,$ 5,$ 6,$ 7 $ 8 $ 09 $ 10 $ 11 $ 12)归还ID[[标题,零],[ created_at,星期三,2013年10月23号11点52分29秒EDT -04:00],[image_path,thumb_Untitled.png],[original_id,零],[位置,0],[ PROJECT_ID,10],[保存,真],[sound_id,零],[step_id的数据类型,97],[的updated_at,星期三,2013年10月23日11时52分29秒EDT -04:00 ],[VIDEO_ID,158],[video_thumbnail,零]
   (0.5毫秒)COMMIT
   (0.1毫秒)BEGIN
   (0.1毫秒)COMMIT
   (0.1毫秒)BEGIN
   (0.1毫秒)COMMIT
   (0.1毫秒)BEGIN
   (0.4ms)UPDATE视频SETimage_id= 348的updated_at='2013年10月23日15:52:30.679474'WHERE视频,ID= 158
   (0.4ms)COMMIT
  项目负荷(0.3ms)选择项目。*从项目WHERE项目,ID= 10 LIMIT 1
  SQL(为0.7m​​s)UPDATE项目SET的updated_at='2013年10月23号15:52:30.684551'WHERE项目,ID= 10
  步骤负荷(0.4ms)选择步骤。* FROM步骤WHERE台阶。ID= 97 LIMIT 1
  图像加载(为0.2ms)选择图像。* FROM图像WHERE图像,step_id的数据类型= 97
  视频负载(为0.2ms),选择视频。* FROM视频WHERE视频,image_id= 348 LIMIT 1
  渲染图像/ _edit_image.html.erb(3.8ms)
  渲染图像/ create.js.erb(12.3ms)
在13069.3ms完成200 OK(浏览次数:15.3ms | ActiveRecord的:64.6​​ms)
 

这是什么在我的网页呈现的:

 &LT;视频控制&GT;
    &lt;信源SRC =HTTPS://...s3.amazonaws.com/video/vi​​deo_path/158/Untitled.webm级=348ID =video_348TYPE =视频/ W​​EBM&GT;
    &lt;信源SRC =HTTPS://...s3.amazonaws.com/video/vi​​deo_path/158/Untitled.mp4级=348ID =video_348TYPE =视频/ MP4&GT;
&LT; /视频&GT;
 

解决方案

我忘了更新我的答案,这一次我想出了一个办法手动设置内容类型。这是我落得这样做:

 要求carrierwave /处理/ mime_types
类VideoPathUploader&LT; CarrierWave ::上传::基地
  包括CarrierWave :: MIMETYPES
  过程:恩code

  DEF EN code
    视频= MiniExiftool.new(@ file.path)
    方向= video.rotation
    model.rotation =方向
    Rails.logger.debug(方向:#{}方向)
    Rails.logger.debug(视频宽x高#{video.imagewidth}#{video.imageheight})

    如果方向== 90安培;&安培; video.imagewidth.to_f&GT; video.imageheight.to_f
      Rails.logger.debug(旋转视频)
      aspect_ratio = video.imageheight.to_f / video.imagewidth.to_f
      EN code_video(:MP4,自定义:-vf转= 1,分辨率:同样的,纵横:aspect_ratio)
    其他
      aspect_ratio = video.imagewidth.to_f / video.imageheight.to_f
      EN code_video(:MP4,分辨率:同样的,纵横:aspect_ratio)
    结束

    instance_variable_set(:@ CONTENT_TYPE,视频/ MP4)
    :set_content_type_mp4
  结束

  版本:WEBM做
    过程:恩code_video =&GT; [:WEBM]
    过程:set_content_type_webm
    高清full_filename(for_file)
      #{File.basename(for_file,File.extname(for_file))}。WEBM
    结束
  结束

 高清set_content_type_mp4(*参数)
    Rails.logger.debug#{file.content_type}
    self.file.instance_variable_set(:@ CONTENT_TYPE,视频/ MP4)
  结束

  高清set_content_type_webm(*参数)
    Rails.logger.debug#{file.content_type}
    self.file.instance_variable_set(:@ CONTENT_TYPE,视频/ W​​EBM)
  结束
结束
 

希望这可以帮助别人!

I am using Carrierwave with fog storage in my Rails app, and I'm using the Carrierwave-Video and Carrierwave-Video-Thumbnailer gems to enable video uploading and thumbnail creation from the uploaded video.

When I upload an mp4 file, I expect it to transcode it to mp4, create a webm version, and create two differently-sized thumbnail images (.png). Right now, all the files are successfully uploaded to Amazon S3, but the content-type is getting processed as video/quicktime for all files. This is causing an issue for me because I want to play webm files in Firefox using the HTML 5 Video tag, and it's not able to play them since the content type of the video is set to video/quicktime rather than video/webm.

This is my uploader file, 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

My rails log when I upload a video:

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)

And this is what is rendered on my page:

<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>

解决方案

I forgot to update my answer to this once I figured out a way to manually set the content type. This is what I ended up doing:

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

Hope this helps someone!

这篇关于carrierwave设置CONTENT_TYPE不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆