一杯清酒邀明月
天下本无事,庸人扰之而烦耳。
posts - 3121,comments - 209,views - 578万

opencv依赖了ffmpeg,所以可以轻松对avi视频文件进行操作。
打开视频文件或摄像头视频需要使用Opencv中的VideoCapture类,保存视频或摄像头视频到本地磁盘,需要使用Opencv中的VideoWriter类。

先上代码:

复制代码
 1 bool isInit= false;
 2 VideoCapture *inputVideo=NULL;
 3 VideoWriter *outputVideo=NULL;
 4 
 5 JNIEXPORT void JNICALL
 6 Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv *, jobject, jlong addrGray,
 7                                                                  jlong addrRgba) {
 8     Mat &mGr = *(Mat *) addrGray;
 9     Mat &mRgb = *(Mat *) addrRgba;
10     vector<KeyPoint> v;
11 
12     looperAddNum++;
13     if (looperAddNum > 15) {
14         looperAddNum = 0;
15         if (looperIndexNum < 100)
16             looperIndexNum += 1;
17     }
18 
19 
20 
21     // code start-----------------------------------------------------------------------------
22 
23     if (!isInit){
24         LOGI("index start");
25         const string source      = "/data/data/org.opencv.samples.tutorial2/cache/Megamind.avi";           // the source file name
26         inputVideo=new VideoCapture(source.c_str());
27         if (!inputVideo->isOpened()){
28             LOGI("open video error");
29             return;
30         }
31         Mat lsFrame;
32         *inputVideo>> lsFrame;
33         if (lsFrame.empty())
34             return;
35         string::size_type pAt = source.find_last_of('.');                  // Find extension point
36         const string NAME = source.substr(0, pAt) + "R" + ".avi";   // Form the new name with container
37         LOGI("index looper %d %d", lsFrame.cols, lsFrame.rows);
38         outputVideo=new VideoWriter();
39         outputVideo->open(NAME, VideoWriter::fourcc('M', 'J', 'P', 'G'), 25, Size(lsFrame.cols, lsFrame.rows), true);
40         if (!outputVideo->isOpened())
41         {
42             LOGI("index open video error %s", NAME.c_str());
43             return;
44         }
45         isInit= true;
46     }
47 
48 
49 
50 
51     if (isInit){
52         Mat videoFrame;
53         *inputVideo >> videoFrame;
54         if (videoFrame.empty())
55             return;
56         // show video
57         Mat dstW = mRgb(Rect(0,0, videoFrame.cols, videoFrame.rows));
58         cvtColor(videoFrame, dstW, COLOR_RGB2BGRA);
59 
60         int channel = 2; // Select the channel to save
61         Mat res;
62         vector<Mat> spl;
63         split(videoFrame, spl);                // process - extract only the correct channel
64         for (int i =0; i < 3; ++i)
65             if (i != channel)
66                 spl[i] = Mat::zeros(Size(videoFrame.cols, videoFrame.rows), spl[0].type());
67         merge(spl, res);
68         *outputVideo << res;
69     }
70 
71 
72     // code end-----------------------------------------------------------------------------
73 }
复制代码

代码说明:
1、使用VideoCapture读内容文件获取视频信息。
2、初始化输出视频VideoWriter。
3、开始读取mat 写入 VideoWriter。
4、同时将其中两个通道数据设置为0(黑色)。

编解码器标识说明:
CV_FOURCC(‘P’, ‘I’, ‘M’, ‘1’) = MPEG-1 codec
CV_FOURCC(‘M’, ‘J’, ‘P’, ‘G’) = motion-jpeg codec
CV_FOURCC(‘M’, ‘P’, ‘4’, ‘2’) = MPEG-4.2 codec
CV_FOURCC(‘D’, ‘I’, ‘V’, ‘3’) = MPEG-4.3 codec
CV_FOURCC(‘D’, ‘I’, ‘V’, ‘X’) = MPEG-4 codec
CV_FOURCC(‘U’, ‘2’, ‘6’, ‘3’) = H263 codec
CV_FOURCC(‘I’, ‘2’, ‘6’, ‘3’) = H263I codec
CV_FOURCC(‘F’, ‘L’, ‘V’, ‘1’) = FLV1 codec

 

 

 

 

posted on   一杯清酒邀明月  阅读(702)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2020-03-25 C++ strcmp与strncmp的比较
2020-03-25 Qt 状态栏(statusbar)的使用
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示