# 解码/编码图像和视频 > 原文:[`pytorch.org/vision/stable/io.html`](https://pytorch.org/vision/stable/io.html) `torchvision.io`包提供了执行 IO 操作的函数。目前这些函数专门用于读取和写入图像和视频。 ## 图像 | `read_image`(path[, mode]) | 将 JPEG 或 PNG 图像读入三维 RGB 或灰度张量。 | | --- | --- | | `decode_image`(input[, mode]) | 检测图像是 JPEG 还是 PNG,并执行适当的操作将图像解码为三维 RGB 或灰度张量。 | | `encode_jpeg`(input[, quality]) | 将输入张量按 CHW 布局编码为其对应 JPEG 文件内容的缓冲区。 | | `decode_jpeg`(input[, mode, device]) | 将 JPEG 图像解码为三维 RGB 或灰度张量。 | | `write_jpeg`(input, filename[, quality]) | 将输入张量按 CHW 布局保存为 JPEG 文件。 | | `encode_png`(input[, compression_level]) | 将输入张量按 CHW 布局编码为其对应 PNG 文件内容的缓冲区。 | | `decode_png`(input[, mode]) | 将 PNG 图像解码为三维 RGB 或灰度张量。 | | `write_png`(input, filename[, compression_level]) | 将输入张量按 CHW 布局(或灰度图像的情况下按 HW 布局)保存为 PNG 文件。 | | `read_file`(path) | 读取文件的字节内容,并输出为具有一维 uint8 张量。 | | `write_file`(filename, data) | 将具有一维的 uint8 张量内容写入文件。 | | `ImageReadMode`(value) | 在读取图像时支持各种模式。 | ## 视频 | `read_video`(filename[, start_pts, end_pts, ...]) | 从文件中读取视频,返回视频帧和音频帧 | | --- | --- | | `read_video_timestamps`(filename[, pts_unit]) | 列出视频帧的时间戳。 | | `write_video`(filename, video_array, fps[, ...]) | 将[T, H, W, C]格式的 4 维张量写入视频文件 | ### 细粒度视频 API 除了`read_video`函数外,我们还提供了一个高性能的低级 API,用于比`read_video`函数更精细的控制。它在完全支持 torchscript 的同时完成所有这些操作。 警告 细粒度视频 API 处于 Beta 阶段,不保证向后兼容性。 | `VideoReader`(src[, stream, num_threads]) | 细粒度视频读取 API。 | | --- | --- | 检查视频的示例: ```py import torchvision video_path = "path to a test video" # Constructor allocates memory and a threaded decoder # instance per video. At the moment it takes two arguments: # path to the video file, and a wanted stream. reader = torchvision.io.VideoReader(video_path, "video") # The information about the video can be retrieved using the # `get_metadata()` method. It returns a dictionary for every stream, with # duration and other relevant metadata (often frame rate) reader_md = reader.get_metadata() # metadata is structured as a dict of dicts with following structure # {"stream_type": {"attribute": [attribute per stream]}} # # following would print out the list of frame rates for every present video stream print(reader_md["video"]["fps"]) # we explicitly select the stream we would like to operate on. In # the constructor we select a default video stream, but # in practice, we can set whichever stream we would like video.set_current_stream("video:0") ```