视频的ffmpeg条件旋转 [英] ffmpeg conditional rotation of video

查看:200
本文介绍了视频的ffmpeg条件旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用载波视频宝石上载纵向影片时遇到问题。当上传人像视频(特别是在移动设备上拍摄的视频)时,它们顺时针旋转90度。在Carrierwave-Video文档中,有一个用于动态配置的选项,但是我没有看到一种方法来动态地传递自定义参数,以便基于视频方向进行视频转码。我知道,如果我运行以下行,我可以旋转视频90度CCW:

I'm having issues uploading portrait videos using the Carrierwave-Video gem. When portrait videos are uploaded (particularly video captured on mobile devices), they are rotated 90 degrees clockwise. In the Carrierwave-Video documentation, there's an option for dynamic configuration, but I don't see a way to dynamically pass custom parameters for transcoding the video based on video orientation. I know that if I run the following line, I'm able to rotate the video 90 degrees CCW:

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

我需要一个可靠的方法检测视频是否需要旋转。我想知道是否有一些方法可以使用ffmpeg来运行一个条件参数,只有当视频是纵向时才会运行。

but I need a reliable way to detect whether the video needs to be rotated or not. I was wondering if there was some way for me to run a conditional parameter with ffmpeg that only runs if the video is portrait.

如果这有帮助,这是我的视频上传器看起来像我的Rails应用程序(由于某种原因,转码过程即使在检测到视频的方向之前也正在运行):

In case it's helpful, this is what my Video Uploader looks like in my Rails app (for some reason, the transcoding process is being run even before it detects the orientation of the video):

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


推荐答案

我能够解决问题使用 mini_exiftool 宝石。在我的电脑上安装exiftool(使用brew install exiftool)后,我能够获得上传视频的方向和宽高比,并使用它来确定是否使用ffmpeg对视频应用转换。这是我的最终上传者:

I was able to solve the problem by using the mini_exiftool gem. After installing exiftool on my computer (using brew install exiftool), I was able to get the orientation and aspect ratio of the uploaded video and use that to determine whether or not to apply a transform to the video using ffmpeg. Here is my final uploader:

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应用程序。我通过使用以下构建包来实现:

Also, in case it's helpful, I also had to install exiftool on Heroku to use it with my Rails app. I did this by using the following buildpack:

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

安装buildpack后,我还是要手动指定exiftool的路径(应该是在安装buildpack时自动执行此操作,但是没有为我做这件事情)。我通过手动设置路径:

After installing the buildpack, I still had to manually specify the path for exiftool (it's supposed to do this automatically when it installs the buildpack, but it didn't do it for me). I did this by manually setting the path:

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

这篇关于视频的ffmpeg条件旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆