2014-02-11 59 views
2

我在使用Carrierwave-Video寶石上傳縱向視頻時遇到了問題。上傳縱向視頻時(尤其是在移動設備上捕捉的視頻)時,順時針旋轉90度。在Carrierwave-Video文檔中,有一個用於動態配置的選項,但我沒有看到動態傳遞自定義參數以基於視頻方向對視頻進行轉碼的方法。我知道,如果我運行以下命令行,我能夠旋轉視頻90度CCW:ffmpeg視頻的條件旋轉

encode_video(:mp4, custom: "-vf transpose=1") 

我需要一個可靠的方式來檢測視頻是否需要旋轉或不。我想知道是否有一些方法可以讓我用ffmpeg運行條件參數,只有在視頻是肖像的情況下才運行。

在情況下,它是有幫助的,這就是我的視頻上傳的樣子在我的Rails應用程序(出於某種原因,轉碼進程正在檢測的視頻的方向,即使之前運行):

require 'carrierwave/processing/mime_types' 

class VideoPathUploader < CarrierWave::Uploader::Base 

    include CarrierWave::Video 
    include CarrierWave::Video::Thumbnailer 
    include CarrierWave::MimeTypes 

process :encode 

def encode 
    Rails.logger.debug "in encode" 
    video = FFMPEG::Movie.new(@file.path) 
    video_width = video.width 
    video_height = video.height 
    Rails.logger.debug "video widthxheight: #{video.width}x#{video.height}" 
    aspect_ratio = video_width/video_height 
    if video_height > video_width 
     # rotate video 
     Rails.logger.debug "portrait video" 
     encode_video(:mp4, custom: "-vf transpose=1", aspect: aspect_ratio) 
    else 
     encode_video(:mp4, aspect: aspect_ratio) 
    end 

    instance_variable_set(:@content_type, "video/mp4") 
    :set_content_type_mp4 
    end 
end 

回答

1

我能夠通過使用mini_exiftool寶石來解決問題。在我的電腦上安裝exiftool後(使用brew install exiftool),我能夠獲得上傳視頻的方向和長寬比,並使用它來確定是否使用ffmpeg將變換應用於視頻。這是我最後上傳:

require 'carrierwave/processing/mime_types' 
require 'rubygems' 
require 'mini_exiftool' 

class VideoPathUploader < CarrierWave::Uploader::Base 

process :encode 

def encode 
    video = MiniExiftool.new(@file.path) 
    orientation = video.rotation 

    if orientation == 90 
     # rotate video 
     Rails.logger.debug "portrait video" 
     aspect_ratio = video.imageheight.to_f/video.imagewidth.to_f 
     encode_video(:mp4, custom: "-vf transpose=1", 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 

end 

此外,如果它是有幫助的,我也只好在Heroku上安裝exiftool與我的Rails應用程序中使用它。我這樣做是使用以下buildpack:

https://github.com/benalavi/buildpack-exiftool

安裝buildpack後,我仍然不得不手動指定exiftool路徑(它應該當它安裝buildpack自動做到這一點,但它沒」不要爲我做)。我這樣做是通過手動設置路徑:

heroku config:set PATH=*all_your_other_paths*:vendor/exiftool-9.40/bin 
+0

benalavi exiftool buildpack的路徑不正確。 bin目錄不包括在內。這就是爲什麼它不起作用。 voodoorai2000分叉並修復路徑([更新路徑提交](https://github.com/voodoorai2000/buildpack-exiftool/commit/0a8c8245d89f38b665f93b84b36baea8746e55d6))。他似乎在爲我工作。 https://github.com/voodoorai2000/buildpack-exiftool – Ari

1

我也有類似的問題,使用基於溶液的我解掉科學的

的你很可能希望清除掉與之相關聯的旋轉元數據視頻。某些玩家(quicktime)會查看旋轉元數據並相應地旋轉視頻。因此,如果您在轉碼時旋轉90度,然後視頻在播放器中旋轉90度,則它將以180度播放。我還添加了一些標誌來提高轉碼視頻的質量。

if orientation == 90 
    aspect_ratio = video.imageheight.to_f/video.imagewidth.to_f 
    encode_video(:mp4, custom: "-qscale 0 -preset slow -g 30 -vf 'transpose=1' -metadata:s:v:0 rotate=0", aspect: aspect_ratio) 
else