OpenCV在Visual Studio中使用c++如何从视频文件中剪切选定的定时
OpenCV in Visual Studio using C++ How to cut selected timing from a video file?
我有一个5分钟的mp4视频文件。我想把2分钟缩短到3分钟。
。Video1.mp4是5分钟长的视频,输出应该是另一个名为Video2.mp4的文件,其中包含Video1.mp4的2到3分钟。
我需要从下面的代码编辑什么?请帮忙吗?
#include "stdafx.h" //By default
#include < string.h>
#include < stdio.h>
#include < stdlib.h>
#include "cv.h"
#include < highgui.h>
#include "cxcore.h"
#include <iostream> //To use cout and cin
using namespace cv; //To use VideoCapture
//Public file path
string fullPath;
VideoCapture capture(0);
void ExplicitWM()
{
//Explicit Watermark
//Input Message
char txt[100] = {' '};
cout << "Enter message to encode: " << endl;
cout<<"-----------------------"<< endl;
cin >> txt;
double dWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
//Save Video to desktop
string userProfile = getenv("userprofile");
string path = userProfile + "\Desktop\";
string saveVid;
getline (cin, saveVid);
string fullPath = path + saveVid;
//CV_FOURCC('P','I','M','1') is a MPEG-1 codec
VideoWriter oVideoWriter (fullPath + "SavedVid.avi", CV_FOURCC('P', 'I', 'M', '1'), 30, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
}
while ( capture.isOpened() )
{
Mat frame;
bool bSuccess = capture.read(frame); // read a new frame from video
imshow("Original Video", frame); //show the frame in "Original Video" window
Mat imgW = frame;
// cvScalar(255,255,255) = rgb
// cvPoint(40,40) = x-y cord
// 0.8 = font size
putText(imgW, txt, cvPoint(400,400), CV_FONT_HERSHEY_COMPLEX, 0.8, cvScalar(255,255,255), 1, CV_AA);
imshow("Watermarked Video", imgW); //show the frame of which watermark added
oVideoWriter.write(imgW); //write the frame into the file
if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "Esc key is pressed by user" << endl;
break;
}
}
}
int main(int argc, char* argv[] )
{
cout<< endl;
cout << "Place video in Desktop and enter file name (e.g. vid.avi):" << endl;
cout<<"--------------------------------------"<< endl;
//Get user desktop file path
string userProfile = getenv("userprofile"); //GetEnvironmentVariable() is to get current userprofile (e.g."C:UsersL30807")
string path = userProfile + "\Desktop\";
string vid;
getline (cin, vid); //Prompt to input file name
fullPath = path + vid;
capture.open(fullPath); //Read video
cout<< endl;
if ( capture.isOpened() )
{
char choice;
cout<< "Choose option (a or b): "<< endl;
cout<<"-----------------------"<< endl;
cout<< "a.Encode Video "<<endl;
cout<< "b.Decode Video "<<endl;
cout<<"-----------------------"<< endl;
cin >> choice;
cout<< endl;
if (choice== 'a')//Encode Video
{
int choiceEn;
cout<< "Choose option (1 or 2): "<< endl;
cout<<"-----------------------"<< endl;
cout<< "1.Explicit Watermarking " <<endl;
cout<< "2.Implicit Watermarking(DCT) "<<endl;
cout<<"-----------------------"<< endl;
cin >> choiceEn;
cout<< endl;
switch ( choiceEn )
{
case 1:
ExplicitWM();
break;
default:
cout<<"Error: Wrong input. Exiting..." << endl;
return 0;
}
}
else if (choice== 'b')
{
}
}
-
使用
将开始时间和停止时间转换为帧数start_frame_count = frame_per_sec * 60 * start_time_in_min;
stop_frame_count = frame_per_sec * 60 * stop_time_in_min; -
计算while循环中的帧数
-
检查计数是否达到启动或停止帧计数
代码如下,
stop_frame_count = int(stop_frame_count+0.5);
frameCount=0;
while(1)
{
frameCount++;
capture.read(frame);
if(frameCount >= start_frame_count && frameCount < stop_frame_count)
{
VideoWriter.write(frame);
}
else if(frameCount == stop_frame_count)
{
break;
}
}
}
相关文章:
- 如何在 c++ 中通过 http 发送大型视频文件?
- 媒体基础将音频流添加到视频文件
- 帧提取 使用OpenCV的视频文件开头只有几秒钟
- 使用 OpenCV C++ 每 1 分钟创建新的视频文件
- 如何检查给定文件是否是C++中的有效视频文件?
- 媒体基础获得视频文件的精确帧(样本)计数
- 如何在不重写整个视频文件的情况下仅替换流中的一个数据框架?(ffmpeg)
- 创建可用的H.264视频文件
- 如何避免在 c++ 中使用 libssh scp 命令压缩从远程 ssh 下载的图像、音频和视频文件?
- 如何使用Windows Media Foundation直接将视频文件直接解码为Direct3D11纹理
- 无法从文件中读取视频,而可以从相机读取
- ARtoolkit加载从4k相机索尼捕获的视频文件
- 视频编写器功能不会使用 OpenCV-3.0.0 保存文件
- 读取视频文件"Open CV WARNING: Couldn't read movie file bird.avi"时出现问题
- 将视频文件的帧总数保存到.txt文件中
- 使用OpenCV从文件中更改视频捕获上的fps
- openCV读取.avi文件的视频捕获获取空框架
- 从OpenCV C 中的视频文件中检索每三个帧
- 使用来自视频文件或网络摄像头的 Kinect 面部跟踪
- 从文件OpenCV读取视频