Clipping a video in C++ OpenCV from a specified start and end point

trim video online
ffmpeg trim end of video
how to trim a video in vlc
ffmpeg trim video by time
ffmpeg cut video by frame
ffmpeg cut video from time to end
trim video vlc mac
how to trim videos on windows media player

I want to clip a video in C++ OpenCV from a specified start and end point. I understand that we can use the following VideoCapture properties for this purpose.

CV_CAP_PROP_POS_MSEC or `CV_CAP_PROP_POS_FRAMES`

But how do I exactly clip a video using any one of the above properties and write it on another video file? I don't find any method to read the video file from a specified relative position in millisecond or from a relative frame index.

I was working on some simple tools like this recently. It builds and runs for me with no issue. Here you go:

// Code borrowed heavily from the OpenCV samples

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/videoio.hpp>

#include <cctype>
#include <stdio.h>
#include <string.h>
#include <time.h>

using namespace cv;
using namespace std;
int main( int argc, char** argv )
{    
    // Command line argument parsing
    cv::CommandLineParser parser(argc, argv,
        "{help h usage ?|               | print this message        }"
        "{@vid_name     |               | video file                }"
        "{@frame_num    | 0             | start frame               }"
        "{@frame_count  | 100           | frames to extract         }"
        );

    if (parser.has("help"))
    {
        parser.printMessage();
        return 0;
    }

    string inputFilename = parser.get<string>("@vid_name");
    int frameNumber = parser.get<int>("@frame_num");
    int frameCount = parser.get<int>("@frame_count");
    if (!parser.check())
    {
        parser.printMessage();
        parser.printErrors();
        return -1;
    }

    // Open video
    VideoCapture capture;
    capture.open(inputFilename);
    if ( !capture.isOpened() )
    {
        fprintf(stderr, "Failed to open video\n");
        return -1;
    }
    double fps = capture.get(CV_CAP_PROP_FPS);
    size_t width = (size_t)capture.get(CV_CAP_PROP_FRAME_WIDTH);
    size_t height = (size_t)capture.get(CV_CAP_PROP_FRAME_HEIGHT);
    Size imgSz(width,height);
    size_t totalFrames = (size_t)capture.get(CV_CAP_PROP_FRAME_COUNT);

    // Open output video files        
    VideoWriter vidwriter;
    int codec = CV_FOURCC('M', 'J', 'P', 'G');
    vidwriter.open("clip.avi", codec, fps, imgSz, true);
    if ( !vidwriter.isOpened() ) 
    {
        fprintf(stderr, "Could not open an output video file for write\n");
        return -1;
    }

    // Extract subset of frames
    Mat frame;
    capture.set(CV_CAP_PROP_POS_FRAMES, frameNumber);
    for (int frameInd = frameNumber; frameInd < frameNumber+frameCount; ++frameInd)
    {
        capture >> frame;
        if ( frame.empty() ) 
            break;
        vidwriter.write(frame);
    }
    capture.release();

    printf("Success.\n");

    return 0;
}

Cutting the videos based on start and end time using ffmpeg, -c copy: This is an option to trim via stream copy. (NB: Very fast) make in instantly. In that case ffmpeg will not re-encode video, just will cut to according size. Good clipping strategy is important in the development of video games in order to maximize the game's frame rate and visual quality. Despite GPU chips that are faster every year, it remains computationally expensive to transform , texture , and shade polygons, especially with the multiple texture and shading passes common today.

After creating a VideoCaptureof your video file, you can use the property CV_CAP_PROP_FPS and calculate easily the frame number of the starting and ending time of the clip you want to cut.

Just create a VideoWriter use the set property CV_CAP_PROP_POS_FRAMES to the frame number of the initial time and start adding frames to the VideoWriter until you reach the second frame number you calculated before.

Using ffmpeg to cut up video, ffmpeg -ss 00:00:30.0 -i input.wmv -c copy -t 00:00:10.0 output.wmv ffmpeg -ss 30 -i ffmpeg -ss 00:08:00 -i Video.mp4 -ss 00:01:00 -t 00:01:00 -c copy VideoClip. mp4 it's possible to cut/trim and keep all streams with the following command but its all is taking more time in cut clip from existing video. i need a fast cut clip code. like "pinnacle". Monday, June 7, 2010 6:54 AM text/html 6/9/2010 7:16:53 AM Harry Zhu 0

Windows 10: How to Trim Video, No no not helpful :( im looking for cropping not trimming. This article is about trimming :( Lisa C says. May 25� To edit a video clip on Windows, start by opening it with Photos. When the program opens, click “Edit and Create” in the top right corner of the window, and select “Create a movie with text” from the drop-down menu. After that, drag your video clip from the upper-left side window to the “Storyboard” section at the bottom.

[PDF] New Video Player Release, So play the video to the time you want to clip and then click on set time. You can move through the video by playing the video, clicking on the scrub bar, or hitting the +/-10s buttons to move in 10 second increments. You can also move the yellow 'handles' that define the clip boundaries. The video cut will be saved automatically by VLC (in Windows 7 the video will be placed in the Videos library). Share your clip with your friends or keep that treasured moment for later. Comments

Trim video files using FFmpeg, So here it goes, this is what I use (on Ubuntu) to trim out parts of a long video file: ffmpeg -i input.mp4 -ss 01:19:27 -to 02:18:51 -c:v copy -c:a� Here you will learn about cohen sutherland line clipping algorithm in C and C++. This is one of the oldest and most popular line clipping algorithm. To speed up the process this algorithm performs initial tests that reduce number of intersections that must be calculated. It does so by using a 4 bit code called as region code or outcodes.

Free Video Cutter, Cut video of any size or format for free. No limits! Trim video free with fast video cutter program. Learn By default, it's a Program Files folder on your C drive. In this video we give you a simplified definition of clipping and how to avoid it. For more Tech Tips be sure to check out The DDownlow link at the end of the video!

Comments
  • Do you expect any quality loss with this code? In other words, will the video be re-encoded? In case it does, do you think there's a way to avoid that?
  • There are other answers that provide the OP's question, and they were posted some time ago. When posting an answer see: How do I write a good answer?, please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions. Please note - the OP posted a C++ OpenCV question.
  • My answer was different from the OP, we use different video property. What's more, Python code is easier to use actually. Just offer another option for the asker.