【问题标题】:ffmpeg conditional rotation of videoffmpeg 有条件地旋转视频
【发布时间】:2014-02-11 18:18:53
【问题描述】:

我在使用 Carrierwave-Video gem 上传肖像视频时遇到问题。上传纵向视频(尤其是在移动设备上拍摄的视频)时,它们会顺时针旋转 90 度。在 Carrierwave-Video 文档中,有一个动态配置选项,但我看不到动态传递自定义参数以根据视频方向对视频进行转码的方法。我知道如果我运行以下行,我可以将视频逆时针旋转 90 度:

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

【问题讨论】:

    标签: ruby-on-rails video ffmpeg carrierwave


    【解决方案1】:

    我能够通过使用mini_exiftool gem 来解决这个问题。在我的计算机上安装 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
    

    【讨论】:

    【解决方案2】:

    我遇到了类似的问题,并使用了基于我的科学解决方案的解决方案

    您很可能希望清除与视频相关的轮播元数据。一些玩家(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
    

    【讨论】:

      猜你喜欢
      • 2020-03-08
      • 2016-07-19
      • 2011-04-25
      • 1970-01-01
      • 2020-01-11
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      相关资源
      最近更新 更多