ffmpeg ubuntu C++ 获取视频的旋转角度

问题:输入的视频有的是左/右旋转90度横拍,有的是竖拍,需要获取视频的旋转角度。

直接上ffmpeg库的解决方法。

1. 安装ffmpeg库

sudo apt-get install ffmpeg

2. 查看安装位置

whereis ffmpeg

我的位置是:

 ffmpeg: /usr/bin/ffmpeg /usr/share/ffmpeg /usr/share/man/man1/ffmpeg.1.gz

3. CMakeLists.txt编写

cmake_minimum_required( VERSION 2.8 )
project(Test)

set( CMAKE_BUILD_TYPE "Release" )
set(CMAKE_CXX_FLAGS "-std=c++11 -O3")

# opencv 
find_package( OpenCV 3.1 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
include_directories("/usr/bin/ffmpeg")

add_library(VO SHARED ./VideoPro.cpp)

link_libraries(VO)
add_executable(Test test.cpp ${VO})
target_link_libraries( Test ${OpenCV_LIBS})
target_link_libraries(Test libavformat.so)

4. 编写读取视频角度函数和头文件

extern "C"
{
#include "libavformat/avformat.h"
};


int getRotateAngle(AVStream* avStream);

int getRotateAngle(AVStream* avStream)
{
    AVDictionaryEntry *tag = NULL;
    int m_Rotate = -1;
    tag = av_dict_get(avStream->metadata, "rotate", tag, 0);
    if (tag == NULL)

    {
        m_Rotate = 0;
    }
    else
    {
        int angle = atoi(tag->value);
        angle %= 360;
        if (angle == 90)
        {
            m_Rotate = 90;

        }
        else if (angle == 180)
        {
            m_Rotate = 180;

        }
        else if (angle == 270)
        {
            m_Rotate = 270;

        }
        else

        {
            m_Rotate = 0;

        }

    }

    return m_Rotate;
}

5. 主函数调用

int main(int argc, char **argv) {
    AVFormatContext *pFormatCtx;
    pFormatCtx = avformat_alloc_context();
    avformat_open_input(&pFormatCtx,argv[1],NULL,NULL);
    AVStream *as = pFormatCtx->streams[0];
    int angle = getRotateAngle(as);
    cout<<"angle: "<<angle<<endl;
}

6. 测试

./Test 1.MOV

返回:

angle: 90

查看视频是左旋转90度横拍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值