Rate This Document
Findability
Accuracy
Completeness
Readability

write_video

Usage

Writes video data in tensor format to a file.

Interface

1
torchvision.io.write_video(filename, video_array, fps, video_codec='libx264', options=None, audio_array=None, audio_fps=None, audio_codec='aac', audio_options=None)

Parameters

Parameter

Description

Value Range

Input/Output

filename

Path to the output video file.

Character string

Input

video_array

Video frame array, with a shape of (T, C, H, W). T indicates the number of frames, C indicates the number of channels (usually 3, standing for RGB), and H and W indicate the height and width, respectively.

torch.Tensor

Input

fps

Video frame rate (frames per second).

(0, INT_MAX]

Input

video_codec

Video encoder. Multiple encoders are available, such as libx264 (default), libx265, and mpeg4.

Character string

Input

options

Extra options of the video encoder, provided in dictionary format. For example, {'crf': '23'} sets the constant rate factor.

Dictionary

Input

audio_array

Audio frame array, with a shape of (T, N). T indicates the number of audio frames, and N indicates the number of audio channels.

torch.Tensor

  

audio_fps

Audio sample rate (frames per second).

(0, INT_MAX]

  

audio_codec

Audio encoder. Multiple encoders are available, such as aac (default) and mp3.

Character string

  

audio_options

Extra options of the audio encoder, provided in dictionary format. For example, {'b:a': '128k'} sets the audio bit rate.

Dictionary

  

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torch
import torchvision.io as io

# Generate a simple video frame array.
video_array = torch.rand((100, 3, 1080, 1920))  # 100 frames, 1080p, RGB

# Generate a simple audio frame array.
audio_array = torch.rand((100000, 2))  # 100,000 samples, dual-channel

# Define the path to the output file.
filename = 'output_video.mp4'

# Write a video file.
io.write_video(
    filename=filename,
    video_array=video_array,
    fps=30,
    video_codec='libx264',
    options={'crf': '23'},
    audio_array=audio_array,
    audio_fps=44100,
    audio_codec='aac',
    audio_options={'b:a': '128k'}
)