Video

Fast & practical AV1 video encoding using ffmpeg

This command allows you to encode videos as AVI with near-realtime speed on commodity computers.

On a more modern AMD server, this works with a AMD Ryzen 5 3600, this works at up to 0.4x speed (~10..15 encode frames per second) for phone video up to ~2.5x for webinar-like video. However, some movies such as shaky phone video, will only work at 0.05x speed = 0.8 encoded frames per second. It really depends on many factors, and also will change a lot during encoding. You need to encode the entire video to judge both the speed and the resulting file size.
On older computers, or for certain movie clips, this can still be horribly slow (Down to ~0.3 encoded frames per second). Consider using H.265 as an alternative, but keep in mind that it’s much, much less space-efficient.

ffmpeg -i input.mp4 -crf 35 -c:v libaom-av1 -usage realtime -tune 0 -c:a libopus -b:a 64k av1.mkv

The most important flag here is -usage realtime which configures the encoder to use fast (as opposed to slow and more efficient) encoding.

-c:a libopus -b:a 64k is optional here but if you’re investing the computing time into re-encoding your video, you might as well re-encode the audio to use a proper codec.

-tune 0 configures the encoder to optimize for visual quality.

See the ffmpeg info page on AV1 for more info. Note that at least on Ubuntu 22.04, only the libaom-av1 encoder is included in the standard ffmpeg version.

Posted by Uli Köhler in Video

How to clone a specific ffmpeg version from git

git clone -b n6.1 --depth 1 https://git.ffmpeg.org/ffmpeg.git ffmpeg

This will clone version 6.1 (-b n6.1) and only perform a shallow clone (i.e. only download the neccessary files for this specific version).

Posted by Uli Köhler in Audio/Video, Video

How to use picamera2 to capture high-resolution image with fixed exposure time

This script will capture a single camera frame from a Raspberry Pi HQ camera with its maximum resolution of 4056x3040px and a fixed exposure time of 10.0ms (which appears to be the minimum exposure time the IMX477 sensor is capable of) and a fixed analog gain of 1.0. The resulting image is saved to Exposure10ms.png

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    camera_config = camera.create_still_configuration({"size":(4056, 3040)})
    camera.configure(camera_config)
    camera.set_controls({"ExposureTime": 10000, "AnalogueGain": 1.0})
    camera.start()
    camera.capture_file("Exposure10ms.png")
    camera.stop()

It appears to be important to use camera.set_control() after camera.configure().

Posted by Uli Köhler in Raspberry Pi, Video

How to livestream Raspberry Pi camera using libcamera-vid & VLC

On the pi, run the following command

libcamera-vid -t0 --width 1920 --height 1080 --framerate 10 --nopreview --codec h264 --profile high --intra 5 --listen -o tcp://0.0.0.0:8494

On the computer where you want to play the livestream, run the following command

vlc tcp/h264://192.168.1.234:8494

where 192.168.1.234 is the IP address of the raspberry pi.

Internally, this uses a raw H.264 stream over TCP. This works best over the local network, but it will also work over VPN or the internet, if your networking setup allows it.

Original source: Github discussion

Posted by Uli Köhler in Raspberry Pi, Video

How to set cv2.VideoCapture() image size in Python

Use cv2.CAP_PROP_FRAME_WIDTH and cv2.CAP_PROP_FRAME_HEIGHT in order to tell OpenCV which image size you would like.

import cv2

video_capture = cv2.VideoCapture(0)
# Check success
if not video_capture.isOpened():
    raise Exception("Could not open video device")
# Set properties. Each returns === True on success (i.e. correct resolution)
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)
# Read picture. ret === True on success
ret, frame = video_capture.read()
# Close device
video_capture.release()

Note that most video capture devices (like webcams) only support specific sets of widths & heights. Use uvcdynctrl -f to find out which resolutions are supported:

$ uvcdynctrl -f
Listing available frame formats for device video0:
Pixel format: YUYV (YUYV 4:2:2; MIME type: video/x-raw-yuv)
  Frame size: 640x480
    Frame rates: 30, 20, 10
  Frame size: 352x288
    Frame rates: 30, 20, 10
  Frame size: 320x240
    Frame rates: 30, 20, 10
  Frame size: 176x144
    Frame rates: 30, 20, 10
  Frame size: 160x120
    Frame rates: 30, 20, 10
Posted by Uli Köhler in OpenCV, Python, Video

How to take a webcam picture using OpenCV in Python

This code opens /dev/video0 and takes a single picture, closing the device afterwards:

import cv2

video_capture = cv2.VideoCapture(0)
# Check success
if not video_capture.isOpened():
    raise Exception("Could not open video device")
# Read picture. ret === True on success
ret, frame = video_capture.read()
# Close device
video_capture.release()

You can also use cv2.VideoCapture("/dev/video0"), but this approach is platform-dependent. cv2.VideoCapture(0) will also open the first video device on non-Linux platforms.

In Jupyter you can display the picture using

import sys
from matplotlib import pyplot as plt

frameRGB = frame[:,:,::-1] # BGR => RGB
plt.imshow(frameRGB)

 

Posted by Uli Köhler in OpenCV, Python, Video

How to build rav1e on Ubuntu

This will fetch and build the current git master of rav1e. The build process has been tested on Ubuntu 18.04 with rav1e git revision 49dcaada4.

sudo apt update
sudo apt -y install cargo git perl nasm cmake clang pkg-config
# Fetch
git clone https://github.com/xiph/rav1e.git
mv rav1e rav1e-git
cd rav1e-git
git submodule update --init
# Build libaom-av1
cmake aom_build/aom -DAOM_TARGET_CPU=x86_64 -DCONFIG_AV1_ENCODER=0 -DENABLE_TESTS=0 -DENABLE_DOCS=0 -DCONFIG_LOWBITDEPTH=1
make -j$(nproc)
# Build rav1e
cargo build --release
# Copy to parent directory
cp target/

After the build finishes, the rav1e executable is placed in the directory where you ran those commands. You can delete the rav1e-git folder

Download the resulting binary for Ubuntu 18.04 x86_64 here

Posted by Uli Köhler in Linux, Video