-
Notifications
You must be signed in to change notification settings - Fork 44
Video cutting challenges
This article explains why cutting (extracting) portions of a video file turns out not to be as simple as it seems.
A typical digital video file usually consists of at least one video track and one audio track. Video and audio data in these tracks are encoded (compressed) by a codec - a special algorithm that performs compression of original audio/video data into format that requires much, much less space. (see [https://en.wikipedia.org/wiki/Video_coding_format]). Usually, codecs are lossy, but lossless codecs are also in use. Which codec is used to encode data in your file depends mainly on how the file was recorded (e.g. by your mobile phone, action camera, drone, etc.) and whether it was edited or not.
Video track is a sequence of frames (still images), for example every second of video may contain 25 or 50 frames. As one can easily calculate, even a short video file contains plenty of frames. In many codes every frame is encoded separately and thus stored with full information. This means one can easily extract every frame data by simply locating place in the file where frame data starts. In order to cut a portion of video between two frames it is enough (simplifying a bit) to copy all selected frames to new file and we are done.
Unfortunately, most modern codecs (e.g. h264), encode frames using only differential information between this frame and next/prev frame (see details in https://en.wikipedia.org/wiki/Video_compression_picture_types). This means that every frame is no longer stored with full information. And this also means cutting frames between two given positions becomes tricky.
Lossless cutting is usually preferred as it has two major advantages:
- it is extremely fast, as we only copy data from one file to another
- quality of the resulting vide file is exactly the same as in source file
Unfortunately, it has also one big disadvantage:
- can be done only on key frames i.e. start and finish on key frame.
Cutting with re-encoding means the original file is decoded (as e.g. for playback) then required frames are collected and encoded again into new file (in most cases using a different codec). Re-encoding is very CPU intensive process thus can take a long time depending on your selection size, CPU, original codec etc. On the other hand, cutting with re-encoding gives best results as video can be cut perfectly at required positions.
Simple video cutter supports both cutting methods described above - lossless cutting and cutting with re-encoding. When file is loaded, you can see gray pattern on the timeline, consisting of lighter and darker boxes. These boxes represent video parts between key frames. If you place your cut on the edge of such box, then it will be placed on a key frame so lossless cut is will be possible. Simple video cutter helps you a bit and snap you selection to nearest key frame (if your pointer is relatively close to a key frame).
Note: if you do not see alternating lighter and darker boxes on your timeline, this means your video file does not contains key frames and thus can be cut lossless at any place.