How to fix file extension to create movie thumbnails using CarrierWave

I want to upload files and convert them to thumbnails.

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

  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.

  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 the video is uploaded, thubmail video is generated very well. But it does not have the png extension. If I download the mp4 file, its thumbnail also has the mp4 extension. but this image can be viewed in a browser.

How to fix the extension problem? Can anyone point out a problem in the code?

+4
source share
1 answer

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

 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 file name :thumb , and then changed the extension from mp4 to png , but you could do something.

For more information, the wiki has a good article on How to: Customize your version names . Browse other wiki pages for more ideas.

+2
source

Source: https://habr.com/ru/post/1394677/


All Articles