问题:输入的视频有的是左/右旋转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度横拍。