最近想從264視頻中提取運動向量(motion vector),ffmpeg的ffplay是可以顯示運動向量在播放畫面中的,但是具體怎麼樣將運動向量提取到文件中似乎沒有給出解決辦法,自己Google了半天才發現一個github的項目,發現其可以提取運動向量到文件中,也可以顯示運動向量在播放的畫面當中。具體的主頁是:
https://github.com/vadimkantorov/mpegflow
其主要的功能也是十分簡單:
./mpegflow examples/mpi_sintel_final_alley_1.avi > examples/alley_1.txt
還有一下幾種模式可供選擇:
在此不多解釋。
然後主要是這個項目的編譯,自己在windows下面編譯似乎是通過了,但是在實際運用的過程中似乎存在問題,只能使用一種模式提取運動向量(--raw模式),其他的幾種模式似乎都沒有成功,所以索性放棄,在Ubuntu16.04下進行編譯。
查看其編譯說明是依賴於FFmpeg軟件包的,所以在編譯之前是要安裝ffmpeg的,ffmpeg其官網上已經有源代碼可供Linux編譯,具體我也是沒怎麼去試,看到作者推薦使用他自己的另一個項目wigwam安裝ffmpeg,也就沒有多想,直接安裝wigwam
wigwam的主頁是:
在其主頁上也很詳細地寫明瞭安裝方法。
第一:
在~/.bashrc文件中末尾添加以下內容:
alias wigwam='python2.7 <($([ -z "$(which curl)" ] && echo "wget -nv -O -" || echo "curl -sS") https://raw.githubusercontent.com/vadimkantorov/wigwam/master/wigwam.py) --repo https://github.com/vadimkantorov/wigwam/tree/master/wigs'
sudo vim ~/.bashrc
修改後保存退出,然後reload一下:
source ~/.bashrc
然後就是進行ffmpeg的安裝:
wigwam init
wigwam install ffmpeg
其中安裝ffmpeg的過程稍微久一點,十分鐘左右。
安裝完後進行mpegflow的編譯,首先得讓make發現你的ffmpeg在什麼地方:
wigwam in
切換到mpegflow的目錄編譯:
make
有一些warning可以忽略,編譯成功後會生成可執行文件mpegflow
最後可以在example文件夾下面驗證以下mpegflow,切換到mpegflow文件夾下面運行bash腳本文件:
$ /\ $ bash extract_mpeg_flow.sh
然後就會生成4個txt文件,具體的命令可以查看extract_mpeg_flow.sh
注意:因爲mpegflow是依賴於FFmpeg的,所以一定要在使用wigwam in命令進入wigwam環境後才能正常運行,不然會提示找不到
一些庫:
../mpegflow: error while loading shared libraries: libavformat.so.57: cannot open shared object file: No such file or directory
../mpegflow: error while loading shared libraries: libavformat.so.57: cannot open shared object file: No such file or directory
../mpegflow: error while loading shared libraries: libavformat.so.57: cannot open shared object file: No such file or directory
../mpegflow: error while loading shared libraries: libavformat.so.57: cannot open shared object file: No such file or directory
終端輸入exit可以退出wigwam環境。
collect2: error: ld returned 1 exit status
Makefile:6: recipe for target 'mpegflow' failed
make: *** [mpegflow] Error 1
根據提示是因爲缺少bz2的庫,所以進行安裝:
$ sudo apt-get install libbz2-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
bzip2-doc
The following NEW packages will be installed:
bzip2-doc libbz2-dev
0 upgraded, 2 newly installed, 0 to remove and 186 not upgraded.
Need to get 324 kB of archives.
After this operation, 556 kB of additional disk space will be used.
Do you want to continue? [Y/n]
安裝之後重新make,又會出現一系列錯誤:$ vim ./Makefile
在LDFLAGS一行的後面加上 -ldl最後進行make,編譯通過,在此記錄一下warning:
mpegflow.cpp: In function ‘void ffmpeg_init()’:
mpegflow.cpp:75:56: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]
AVCodecContext *enc = ffmpeg_pFormatCtx->streams[i]->codec;
^
In file included from mpegflow.cpp:10:0:
/usr/local/include/libavformat/avformat.h:893:21: note: declared here
AVCodecContext *codec;
^
mpegflow.cpp:75:56: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]
AVCodecContext *enc = ffmpeg_pFormatCtx->streams[i]->codec;
^
In file included from mpegflow.cpp:10:0:
/usr/local/include/libavformat/avformat.h:893:21: note: declared here
AVCodecContext *codec;
^
mpegflow.cpp:75:56: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]
AVCodecContext *enc = ffmpeg_pFormatCtx->streams[i]->codec;
^
In file included from mpegflow.cpp:10:0:
/usr/local/include/libavformat/avformat.h:893:21: note: declared here
AVCodecContext *codec;
^
mpegflow.cpp: In function ‘bool process_frame(AVPacket*)’:
mpegflow.cpp:102:12: warning: ‘int avcodec_decode_video2(AVCodecContext*, AVFrame*, int*, const AVPacket*)’ is deprecated [-Wdeprecated-declarations]
int ret = avcodec_decode_video2(ffmpeg_pVideoStream->codec, ffmpeg_pFrame, &got_frame, pkt);
^
In file included from mpegflow.cpp:9:0:
/usr/local/include/libavcodec/avcodec.h:4901:5: note: declared here
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
^
mpegflow.cpp:102:12: warning: ‘int avcodec_decode_video2(AVCodecContext*, AVFrame*, int*, const AVPacket*)’ is deprecated [-Wdeprecated-declarations]
int ret = avcodec_decode_video2(ffmpeg_pVideoStream->codec, ffmpeg_pFrame, &got_frame, pkt);
^
In file included from mpegflow.cpp:9:0:
/usr/local/include/libavcodec/avcodec.h:4901:5: note: declared here
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
^
mpegflow.cpp:102:55: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]
int ret = avcodec_decode_video2(ffmpeg_pVideoStream->codec, ffmpeg_pFrame, &got_frame, pkt);
^
In file included from mpegflow.cpp:10:0:
/usr/local/include/libavformat/avformat.h:893:21: note: declared here
AVCodecContext *codec;
^
mpegflow.cpp:102:55: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]
int ret = avcodec_decode_video2(ffmpeg_pVideoStream->codec, ffmpeg_pFrame, &got_frame, pkt);
^
In file included from mpegflow.cpp:10:0:
/usr/local/include/libavformat/avformat.h:893:21: note: declared here
AVCodecContext *codec;
^
mpegflow.cpp:102:55: warning: ‘AVStream::codec’ is deprecated [-Wdeprecated-declarations]
int ret = avcodec_decode_video2(ffmpeg_pVideoStream->codec, ffmpeg_pFrame, &got_frame, pkt);
^
In file included from mpegflow.cpp:10:0:
/usr/local/include/libavformat/avformat.h:893:21: note: declared here
AVCodecContext *codec;
^
mpegflow.cpp:102:92: warning: ‘int avcodec_decode_video2(AVCodecContext*, AVFrame*, int*, const AVPacket*)’ is deprecated [-Wdeprecated-declarations]
int ret = avcodec_decode_video2(ffmpeg_pVideoStream->codec, ffmpeg_pFrame, &got_frame, pkt);
^
In file included from mpegflow.cpp:9:0:
/usr/local/include/libavcodec/avcodec.h:4901:5: note: declared here
int avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture,
^
mpegflow.cpp: In function ‘bool read_frame(int64_t&, char&, std::vector<AVMotionVector>&)’:
mpegflow.cpp:154:23: warning: ‘AVFrame::pkt_pts’ is deprecated [-Wdeprecated-declarations]
pts = ffmpeg_pFrame->pkt_pts != AV_NOPTS_VALUE ? ffmpeg_pFrame->pkt_pts : (ffmpeg_pFrame->pkt_dts
^
In file included from /usr/local/include/libavcodec/avcodec.h:38:0,
from mpegflow.cpp:9:
/usr/local/include/libavutil/frame.h:279:13: note: declared here
int64_t pkt_pts;
^
mpegflow.cpp:154:23: warning: ‘AVFrame::pkt_pts’ is deprecated [-Wdeprecated-declarations]
pts = ffmpeg_pFrame->pkt_pts != AV_NOPTS_VALUE ? ffmpeg_pFrame->pkt_pts : (ffmpeg_pFrame->pkt_dts
^
In file included from /usr/local/include/libavcodec/avcodec.h:38:0,
from mpegflow.cpp:9:
/usr/local/include/libavutil/frame.h:279:13: note: declared here
int64_t pkt_pts;
^
mpegflow.cpp:154:23: warning: ‘AVFrame::pkt_pts’ is deprecated [-Wdeprecated-declarations]
pts = ffmpeg_pFrame->pkt_pts != AV_NOPTS_VALUE ? ffmpeg_pFrame->pkt_pts : (ffmpeg_pFrame->pkt_dts
^
In file included from /usr/local/include/libavcodec/avcodec.h:38:0,
from mpegflow.cpp:9:
/usr/local/include/libavutil/frame.h:279:13: note: declared here
int64_t pkt_pts;
^
mpegflow.cpp:154:66: warning: ‘AVFrame::pkt_pts’ is deprecated [-Wdeprecated-declarations]
pts = ffmpeg_pFrame->pkt_pts != AV_NOPTS_VALUE ? ffmpeg_pFrame->pkt_pts : (ffmpeg_pFrame->pkt_dts
^
In file included from /usr/local/include/libavcodec/avcodec.h:38:0,
from mpegflow.cpp:9:
/usr/local/include/libavutil/frame.h:279:13: note: declared here
int64_t pkt_pts;
^
mpegflow.cpp:154:66: warning: ‘AVFrame::pkt_pts’ is deprecated [-Wdeprecated-declarations]
pts = ffmpeg_pFrame->pkt_pts != AV_NOPTS_VALUE ? ffmpeg_pFrame->pkt_pts : (ffmpeg_pFrame->pkt_dts
^
In file included from /usr/local/include/libavcodec/avcodec.h:38:0,
from mpegflow.cpp:9:
/usr/local/include/libavutil/frame.h:279:13: note: declared here
int64_t pkt_pts;
^
mpegflow.cpp:154:66: warning: ‘AVFrame::pkt_pts’ is deprecated [-Wdeprecated-declarations]
pts = ffmpeg_pFrame->pkt_pts != AV_NOPTS_VALUE ? ffmpeg_pFrame->pkt_pts : (ffmpeg_pFrame->pkt_dts
^
In file included from /usr/local/include/libavcodec/avcodec.h:38:0,
from mpegflow.cpp:9:
/usr/local/include/libavutil/frame.h:279:13: note: declared here
int64_t pkt_pts;