| Title: | 'AV1' Video Encoding for Biological Microscopy Data |
|---|---|
| Description: | Converts legacy microscopy video formats (H.264/H.265, AVI/MJPEG, TIFF stacks) to the modern 'AV1' codec with minimal quality loss. Typical use cases include compressing large TIFF stacks from confocal microscopy and time-lapse experiments from hundreds of gigabytes to manageable sizes, re-encoding MP4 files exported from 'CellProfiler', 'ImageJ'/'Fiji', and microscope software with approximately 2x better compression at the same visual quality, and converting legacy AVI (MJPEG) and H.265 recordings to a single patent-free format suited for long-term archival. Automatically selects the best available backend: GPU hardware acceleration via 'Vulkan' 'VK_KHR_VIDEO_ENCODE_AV1' or 'VAAPI' (tested on AMD RDNA4; bundled headers, builds with any 'Vulkan' SDK >= 1.3.275), with automatic fallback to CPU encoding through 'FFmpeg' and 'SVT-AV1'. User controls quality via a single CRF parameter; each backend adapts automatically (CPU and Vulkan use CRF directly, VAAPI targets 55 percent of input bitrate). TIFF stacks use near-lossless CRF 5 by default, with optional proportional scaling via tiff_scale (multiplier or bounding box, aspect ratio always preserved). Small frames are automatically scaled up to meet hardware encoder minimums. Audio tracks are preserved automatically. Provides a simple R API for batch conversion of entire experiment folders. |
| Authors: | Yuri Baramykov [aut, cre] (ORCID: <https://orcid.org/0009-0000-7627-4217>) |
| Maintainer: | Yuri Baramykov <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.3 |
| Built: | 2026-06-01 21:00:36 UTC |
| Source: | https://github.com/zabis13/av1r |
Converts legacy microscopy video formats (H.264/H.265, AVI/MJPEG, TIFF stacks) to the modern 'AV1' codec with minimal quality loss. Typical use cases include compressing large TIFF stacks from confocal microscopy and time-lapse experiments from hundreds of gigabytes to manageable sizes, re-encoding MP4 files exported from 'CellProfiler', 'ImageJ'/'Fiji', and microscope software with approximately 2x better compression at the same visual quality, and converting legacy AVI (MJPEG) and H.265 recordings to a single patent-free format suited for long-term archival. Automatically selects the best available backend: GPU hardware acceleration via 'Vulkan' 'VK_KHR_VIDEO_ENCODE_AV1' or 'VAAPI' (tested on AMD RDNA4; bundled headers, builds with any 'Vulkan' SDK >= 1.3.275), with automatic fallback to CPU encoding through 'FFmpeg' and 'SVT-AV1'. User controls quality via a single CRF parameter; each backend adapts automatically (CPU and Vulkan use CRF directly, VAAPI targets 55 percent of input bitrate). TIFF stacks use near-lossless CRF 5 by default, with optional proportional scaling via tiff_scale (multiplier or bounding box, aspect ratio always preserved). Small frames are automatically scaled up to meet hardware encoder minimums. Audio tracks are preserved automatically. Provides a simple R API for batch conversion of entire experiment folders.
Maintainer: Yuri Baramykov [email protected] (ORCID)
Useful links:
AV1R encoding options
av1r_options( crf = 28L, preset = 8L, threads = 0L, bitrate = NULL, tiff_crf = 5L, tiff_scale = NULL, verbose = TRUE, backend = "auto" )av1r_options( crf = 28L, preset = 8L, threads = 0L, bitrate = NULL, tiff_crf = 5L, tiff_scale = NULL, verbose = TRUE, backend = "auto" )
crf |
Constant Rate Factor: 0 (best) to 63 (worst). Default 28.
Lower = better quality, larger file. Used as fallback when |
preset |
Encoding speed preset: 0 (slowest/best) to 13 (fastest). Default 8 (good balance for microscopy batch jobs). |
threads |
Number of CPU threads. 0 = auto-detect. |
bitrate |
Target video bitrate in kbps (e.g. |
tiff_crf |
CRF for TIFF/image sequence input. Default 5
(near-lossless, suitable for microscopy data). Overrides |
tiff_scale |
Scale factor or target size for TIFF/image input.
|
verbose |
Logical. If |
backend |
|
A named list of encoding parameters.
# Default options (auto-detect backend and bitrate) av1r_options() # High-quality lossless-ish for publication figures av1r_options(crf = 15, preset = 4) # Explicit bitrate (GPU VAAPI) av1r_options(bitrate = 2000, backend = "vaapi") # Fast batch conversion of large TIFF stacks av1r_options(crf = 32, preset = 12, threads = 16)# Default options (auto-detect backend and bitrate) av1r_options() # High-quality lossless-ish for publication figures av1r_options(crf = 15, preset = 4) # Explicit bitrate (GPU VAAPI) av1r_options(bitrate = 2000, backend = "vaapi") # Fast batch conversion of large TIFF stacks av1r_options(crf = 32, preset = 12, threads = 16)
Prints the active encoding backend and available GPU/CPU capabilities.
av1r_status()av1r_status()
Invisibly returns the active backend string.
Finds all supported video files in input_dir and converts them to
AV1. Output files are written to output_dir with the same base name
and .mp4 extension.
convert_folder( input_dir, output_dir = input_dir, options = av1r_options(), ext = c("mp4", "avi", "mkv", "mov", "flv", "mpg", "mpeg", "webm", "tif", "tiff"), skip_existing = TRUE, recursive = FALSE, max_depth = 5L )convert_folder( input_dir, output_dir = input_dir, options = av1r_options(), ext = c("mp4", "avi", "mkv", "mov", "flv", "mpg", "mpeg", "webm", "tif", "tiff"), skip_existing = TRUE, recursive = FALSE, max_depth = 5L )
input_dir |
Path to folder with input files. |
output_dir |
Path to folder for output files. Created if it does not
exist. Defaults to |
options |
An |
ext |
Character vector of input extensions to process.
Default: |
skip_existing |
If |
recursive |
If |
max_depth |
Maximum subfolder depth when |
When a folder contains only single-page TIFF images (e.g. a microscopy image sequence), they are automatically combined into a single video named after the input folder.
When recursive = TRUE, subfolders (up to max_depth levels)
are scanned. Each subfolder containing only TIFF images is combined into
a single video named after the subfolder. Subfolders with video files are
processed file-by-file. All output is written flat into output_dir,
with subfolder names used as prefixes to avoid collisions.
Invisibly returns a data.frame with columns input,
output, input_size (bytes), status
("ok", "skipped", or "error"), and message.
## Not run: # Requires FFmpeg installed convert_folder("~/data/microscopy", file.path(tempdir(), "av1_output")) # Recursive: each subfolder with TIFFs becomes a separate video convert_folder("~/data/plates", "~/av1_output", recursive = TRUE) ## End(Not run)## Not run: # Requires FFmpeg installed convert_folder("~/data/microscopy", file.path(tempdir(), "av1_output")) # Recursive: each subfolder with TIFFs becomes a separate video convert_folder("~/data/plates", "~/av1_output", recursive = TRUE) ## End(Not run)
Converts biological microscopy video files (MP4/H.264, H.265, AVI/MJPEG)
or TIFF stacks to AV1 format. Automatically selects the best available
backend: Vulkan GPU (VK_KHR_VIDEO_ENCODE_AV1) when a compatible
device is found, otherwise CPU via FFmpeg (libsvtav1).
convert_to_av1(input, output, options = av1r_options())convert_to_av1(input, output, options = av1r_options())
input |
Path to input file. Supported: .mp4, .avi, .mkv, .mov, .flv, .mpg, .mpeg, .webm,
.tif/.tiff (multi-page), or printf pattern like |
output |
Path to output file (.mp4 or .mkv). |
options |
An |
Invisibly returns 0L on success. Stops with an error on failure.
# List available options str(av1r_options()) ## Not run: # Requires FFmpeg installed convert_to_av1("recording.mp4", file.path(tempdir(), "recording_av1.mp4")) ## End(Not run)# List available options str(av1r_options()) ## Not run: # Requires FFmpeg installed convert_to_av1("recording.mp4", file.path(tempdir(), "recording_av1.mp4")) ## End(Not run)
Priority order: "vaapi" (VAAPI AV1 GPU via ffmpeg, best
speed/quality ratio) > "vulkan" (Vulkan AV1 GPU, fastest but
CQP only) > "cpu" (SVT-AV1 via ffmpeg).
detect_backend(prefer = "auto")detect_backend(prefer = "auto")
prefer |
|
Character string: "vaapi", "vulkan", or "cpu".
Compares encoded video against the original using SSIM. Values close to 1.0 indicate high similarity.
measure_ssim(original, encoded, duration = NULL)measure_ssim(original, encoded, duration = NULL)
original |
Path to original video file. |
encoded |
Path to encoded video file. |
duration |
Seconds to compare. |
Numeric SSIM score (0-1), or NA on failure.
Does not load pixel data into R – only inspects the file. Useful for checking frame count and dimensions before encoding.
read_tiff_stack(path)read_tiff_stack(path)
path |
Path to a multi-page TIFF file or printf-pattern
(e.g. |
A list with elements: path, n_frames,
width, height, size_mb.
# Create a small temporary TIFF and inspect it tmp <- tempfile(fileext = ".tif") writeBin(raw(1024), tmp) info <- read_tiff_stack(tmp) str(info) unlink(tmp)# Create a small temporary TIFF and inspect it tmp <- tempfile(fileext = ".tif") writeBin(raw(1024), tmp) info <- read_tiff_stack(tmp) str(info) unlink(tmp)
Check Vulkan AV1 availability
vulkan_available()vulkan_available()
TRUE if the package was compiled with Vulkan AV1 encode support.
List Vulkan-capable GPU devices
vulkan_devices()vulkan_devices()
Character vector of device names. Devices supporting
VK_KHR_VIDEO_ENCODE_AV1 are marked with [AV1].