I need to split big video file into smaller pieces by time. Give me your suggestions, please, and if you can some tips for library usage. Thanks.
Asked
Active
Viewed 2.0k times
2 Answers
7
OpenCV has Python wrappers.
As you're interested in video IO, have a look at QueryFrame and its related functions there.
In the end, your code will look something like this (completely untested):
import cv
capture = cv.CaptureFromFile(filename)
while Condition1:
# Need a frame to get the output video dimensions
frame = cv.RetrieveFrame(capture) # Will return None if there are no frames
# New video file
video_out = cv.CreateVideoWriter(output_filenameX,
CV_FOURCC('M','J','P','G'), capture.fps, frame.size(), 1)
# Write the frames
cv.WriteFrame(video_out, frame)
while Condition2:
# Will return None if there are no frames
frame = cv.RetrieveFrame(capture)
cv.WriteFrame(video_out, frame)
By the way, there are also ways to do this without writing any code.

Mr_and_Mrs_D
- 32,208
- 39
- 178
- 361

mpenkov
- 21,621
- 10
- 84
- 126
-
-
2No problem. In general, comments like "thanks, this is what I need" are better made by accepting the answer (click the tick symbol). Glad you could get this worked out. Welcome to stackoverflow. – mpenkov Jan 02 '11 at 22:14
-
This was extremely helpful. I've been looking for a simple and clear library and code example for building a video and this was perfect. Thanks! – Dan Roberts May 06 '11 at 12:37
-
It does not work for me, raises error. First: `GrabFrame` returns an int (always 1 for me) and it is not applicable to `WriteFrame` because it needs `IplImage`. So i changed it to `QueryFrame` according to the [documentation](http://opencv.willowgarage.com/documentation/python/highgui_reading_and_writing_images_and_video.html?highlight=createvideowriter#queryframe). Now it runs without errors, but the generated video has zero length, unreadable by any players. – Lepi Nov 25 '11 at 15:20
-
@Lepi: you're right. It should be `GrabFrame` followed by `QueryFrame` (if `GrabFrame` returns 1) or simply `RetrieveFrame` (which will return `None` if there is no frame available). `QueryFrame` by itself may not be enough. I've corrected the answer. As far as your video having zero length, it's more likely to be a problem with your local install of OpenCV (it needs proper ffmpeg support to write video) than a problem with the above source. You may want to try other codecs (other than MJPG) when creating the `VideoWriter` instance. – mpenkov Nov 26 '11 at 01:29
1
Check youtube-upload, it splits the videos using ffmpeg.
Youtube-upload is a command-line script that uploads videos to Youtube. If a video does not comply with Youtube limitations (<2Gb and <15'), it will be automatically splitted before uploading. Youtube-upload should work on any platform (GNU/Linux, BSD, OS X, Windows, ...) that runs Python and FFmpeg.

tokland
- 66,169
- 13
- 144
- 170