【问题标题】:How to correct file extension for thumbnail generation of movie with CarrierWave如何更正使用 CarrierWave 生成​​电影缩略图的文件扩展名
【发布时间】:2012-12-12 23:45:18
【问题描述】:

我想上传文件并为其转换缩略图。

我的代码是:

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, 70], :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 扩展名。但这是可以在浏览器中查看的图像。

如何纠正扩展问题?谁能指出代码中的问题?

【问题讨论】:

    标签: ruby file-upload ruby-on-rails-3.1 carrierwave


    【解决方案1】:

    我最近通过覆盖 :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 文件名,然后将扩展名从mp4 更改为png,但你可以做任何事情。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-23
      • 2011-12-23
      • 2011-05-14
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多