Video Editing

How can you best edit a video file for analytical usage? In the following, we will go through some editing principles and look at relevant software.

Editing principles

We will here not deal with "normal" video editing procedures (such as fading between clips, etc.) but focus on those that are necessary to prepare video files for analysis. Then the aim is to create as straightforward video files as possible.

Trimming vs cropping

Two useful video editing strategies are often confused:

  • Trimming is the process of cutting out a temporal part of the video. For example, I want to trim the video from time 00:01:27 to 00:08:21.
  • Cropping is the process of cutting a spatial part of the video. For example, to crop out one person of a video image.

Both trimming and cropping are available in most video editing software. Note, however, that cropping can be tricky, since it will (by design) reduce the number of pixels in an image.

Pixel reduction

The number of pixels in a video file influences how large the file is and how heavy it is to process. The latter is usually the most critical. Storage space is relatively cheaper than processing speed. The challenge is to balance between the concern of losing information and gaining processing speed.

Video Editing Software

There are many advanced (and costly) video editing applications available. However, in most cases, it is possible to use either in-built or free software. Here are some of the free/cheap solutions available:

  • Windows: Movie Maker
  • OS X: iMovie
  • Linux: KDenlive

Such software often claim to be non-destructive. That means that they do not modify the original files. However, as soon as they export a video file, that file is recompressed. And compression is a destructive editing technique. Usually it is possible to compress a file a couple of times without loosing too much quality. But one should always be careful about how many times one compresses a video file, and the settings that are used in the compression.

Non-destructive video editors

There are some video editors that can work non-destructively. However, they are usually very limited. One good example is VidCutter, which is tool for trimming videos. So if one only needs to trim out a part of a video file, this is a good tool.

FFmpeg commands

FFmpeg is a command-line video editing tool available for most operating systems. It is very powerful and may feel somewhat intimidating at first. But once you learn some of the tricks, FFmpeg can be used for a number of different things. You do not necessarily need to understand everything that goes on, often it is sufficient to copy and modify "one-liners" to get useful results.

Trimming in FFmpeg

You can split and trim files in most graphical video editing software, but these will typically also recompress the file on export. This reduces the quality of the video, and it also takes a long time. A much better solution is to perform “lossless” trimming. Fortunately, there is a way to do this with the wonderful command-line utility FFmpeg. It is available for most platforms and has a ton of different options. So to remember how it is done, here is a simple one-liner:

ffmpeg -i input.mp4 -ss 01:19:27 -to 02:18:51 -c:v copy -c:a copy output.mp4

This will cut out the section from about 1h19min (after the -ss command) to 2h18min (after the -to command). The copy parts of the command are meant to copy both the original audio and video content without recompressing. This means that the above command only takes a few seconds to run.

You may instead want to specify a fixed duration to extract, in which case you can use:

ffmpeg -i input.mp4 -ss 00:01:10 -t 00:01:05 -c:v copy -c:a copy output.mp4

This will extract 1min5sec (using the -t flag) starting from 1min10sec (the -ss flag) in the file.

 

Convert between video containers with FFmpeg

 

There are many ways of converting video files. In most cases, you would end up with a lossy conversion. That means that the video content will be altered. The file size may be smaller, but the quality may also be worse. The general rule is that you want to compress a file as few times as possible.

 

This little trick converts from one format to another without modifying the content of the file, only the container. That can be achieved with the code shown on top:

ffmpeg -i original.mov -acodec copy -vcodec copy outfile.mp4

There are several benefits of doing it this way:

  1. Quality. Avoiding an unnecessary re-compression of the content, which would only degrade the content.
  2. Preserve the pixel size, sampling rates, etc. of the originals. Most video software will use standard settings for these. I often work with various types of non-standard video files, so it is nice to preserve this information.
  3. Save time. Since no re-compression is needed, we only copy content from one container to another. This is much, much faster than re-compressing the content.

All in all, this long explanation of a short command may help to improve your workflows and save some time.

Converting MXF files to MP4 with FFmpeg

We have a bunch of Canon XF105 at RITMO, a camera that records MXF files. This is not a particularly useful file format (unless for further processing). Since many of our recordings are just for documentation purposes, we often see the need to convert to MP4. Here I present two solutions for converting MXF files to MP4, both as individual files and a combined file from a folder. These are shell scripts based on the handy FFmpeg.

Convert individual MXF files to individual MP4 files

The first solution is based on a script that converts a bunch of MXF files to individual MP4 files. This is practical if there are multiple, single shots. Save the script as mxf2mp4.sh, make it executable, with a command like:

chmod u+x mxf2mp4.sh

and run the file:

./mxf2mp4.sh

Convert a folder of MXF files to one MP4 file

The second solution is when we have made one long recording, which is split into individual MXF files of 1.9 GB size (the maximum size of FAT32-formatted drives) in the camera. Then the aim is to merge all of these to one MP4 file. This script will do the trick. Do the same as above to run the script.

“Flattening” Ricoh Theta 360-degree videos using FFmpeg

Ricoh Theta 360-degree camera.
Now I wanted to see if I could “flatten” a 360-degree video recorded with a Ricoh Theta camera. These cameras contain two fisheye lenses, capturing two 180-degree videos next to each other. This results in video files like shown in the screenshot below.
Screenshot from a video recorded with a Ricoh Theta.

These files are not very useful to watch or work with, so we need to somehow “flatten” it into a more meaningful video file. I find it cumbersome to do this in the Ricoh mobile phone apps, so have been looking for a simple solution to do it on my computer.

I see that the FFmpeg developers are working on native support for various 360-degree video files. This is implemented in the filter v360, but since it is not in the stable version of FFmpeg yet, I decided to look for something that works right now. Then I came across this blog post, which shows how to do the flattening based on two so-called PGM files that contain information about how the video should be mapped:

ffmpeg -i ricoh_input.mp4 -i xmap_thetaS_1920x960v3.pgm -i ymap_ThetaS_1920x960v3.pgm -q 0 -lavfi "format=pix_fmts=rgb24,remap" remapped.mp4

The end result is a flattened video file, as shown below:

Screenshot from a “flattened” 360 degree video.

As for where to split up the video (it is a continuous 360-degree video after all) I will have to investigate later.

Published Feb. 7, 2021 3:31 PM - Last modified Mar. 2, 2021 9:46 AM