加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何使用CarrierWave更正用于缩略图生成电影的文件扩展

发布时间:2020-12-17 02:12:47 所属栏目:百科 来源:网络整理
导读:我想上传文件并转换缩略图. 我的代码是: require 'streamio-ffmpeg'module CarrierWave module FFMPEG module ClassMethods def resample(bitrate) process :resample = bitrate end def gen_video_thumb(width,height) process :gen_video_thumb = [width,
我想上传文件并转换缩略图.

我的代码是:

require 'streamio-ffmpeg'
module CarrierWave
  module FFMPEG
    module ClassMethods
      def resample(bitrate)
        process :resample => bitrate
      end

      def gen_video_thumb(width,height)
        process :gen_video_thumb => [width,height]
      end
    end

    #def is_video?
    #  ::FFMPEG::Movie.new(File.open(store_path)).frame_rate != nil
    #end

    def gen_video_thumb(width,height)
      directory = File.dirname(current_path)
      tmpfile = File.join(directory,"tmpfile")

      FileUtils.move(current_path,tmpfile)
      file = ::FFMPEG::Movie.new(tmpfile)
      file.transcode(current_path,"-ss 00:00:01 -an -r 1 -vframes 1 -s #{width}x#{height}")

      FileUtils.rm(tmpfile)
    end

    def resample(bitrate)
      directory = File.dirname(current_path)
      tmpfile = File.join(directory,"tmpfile")

      File.move(current_path,tmpfile)

      file = ::FFMPEG::Movie.new(tmpfile)
      file.transcode(current_path,:audio_bitrate => bitrate)

      File.delete(tmpfile)
    end
  end
end

我的上传者有

version :thumb do
    process :resize_to_fill => [100,70],:if=> :image?
    process :gen_video_thumb => [100,:if=> :video? do
      process :convert => 'png'
    end
  end

和功能是.

protected

  def image?(new_file)
    ::FFMPEG::Movie.new(new_file.file.path).frame_rate == nil
  end

  def video?(new_file)
    ::FFMPEG::Movie.new(new_file.file.path).frame_rate != nil
  end

但问题是,视频上传,视频thubmail生成非常好.但它没有png扩展名.如果我上传一个mp4文件,它的缩略图也有一个mp4扩展名.但这是一个可以在浏览器中查看的图像.

如何纠正扩展问题?任何人都可以在代码中指出问题吗?

解决方法

我最近通过覆盖:thumb版本的full_filename方法解决了这个问题

version :thumb do
  # do your processing
  process :whatever

  # redefine the name for this version
  def full_filename(for_file=file)
    super.chomp('mp4') + 'png'
  end
end

我调用super来获取默认值:thumb filename,然后将扩展名从mp4更改为png,但你可以做任何事情.

有关更多信息,carrierwave wiki在How to: Customize your version file names上有一篇很好的文章.查看其他维基页面了解很多想法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读