Nazar Hussain
Nazar Hussain

Reputation: 5162

How to correct file extension for thumbnail generation of movie with CarrierWave

I want to upload files and convert thumbnails for it.

My code is:

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

My Uploader have

  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

and functions are.

  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

But the problem is that, video is uploaded, video thubmail is generated very good. But it do not have a png extension. If i upload a mp4 file, its thumbnail also have a mp4 extension. but that is an image can be viewed in browser.

How to correct the extension issue? Can any one point out the issue in the code?

Upvotes: 4

Views: 1068

Answers (1)

Anson
Anson

Reputation: 6750

I recently solved this by overriding the full_filename method for the :thumb version

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

I called super to get the default :thumb filename, then changed the extension from mp4 to png, but you could do anything.

For more info, the carrierwave wiki has a good article on How to: Customize your version file names. Check out the other wiki pages for lots of ideas.

Upvotes: 2

Related Questions