File size: 2,201 Bytes
434b0b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
# @Organization  : Tongyi Lab, Alibaba
# @Author        : Lingteng Qiu
# @Email         : 220019047@link.cuhk.edu.cn
# @Time          : 2025-08-31 10:02:15
# @Function      : FFmpeg video encoding from image sequences

import os

import imageio.v3 as iio
import numpy as np
import torch

VIDEO_TYPE_LIST = {".avi", ".mp4", ".gif", ".AVI", ".MP4", ".GIF"}


def encodeffmpeg(inputs, frame_rate, output, format="png"):
    """output: need video_name"""
    assert (
        os.path.splitext(output)[-1] in VIDEO_TYPE_LIST
    ), "output is the format of video, e.g., mp4"
    assert os.path.isdir(inputs), "input dir is NOT file format"

    inputs = inputs[:-1] if inputs[-1] == "/" else inputs

    output = os.path.abspath(output)

    cmd = (
        f"ffmpeg -r {frame_rate} -pattern_type glob -i '{inputs}/*.{format}' "
        + f'-vcodec libx264 -crf 10 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" '
        + f"-pix_fmt yuv420p {output} > /dev/null 2>&1"
    )

    print(cmd)

    output_dir = os.path.dirname(output)
    if os.path.exists(output):
        os.remove(output)
    os.makedirs(output_dir, exist_ok=True)

    print("encoding imgs to video.....")
    os.system(cmd)
    print("video done!")


def images_to_video(
    images, output_path, fps, gradio_codec: bool, verbose=False, bitrate="10M"
):
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    frames = []
    for i in range(images.shape[0]):
        if isinstance(images, torch.Tensor):
            frame = (images[i].permute(1, 2, 0).cpu().numpy() * 255).astype(np.uint8)
            assert (
                frame.shape[0] == images.shape[2] and frame.shape[1] == images.shape[3]
            ), f"Frame shape mismatch: {frame.shape} vs {images.shape}"
            assert (
                frame.min() >= 0 and frame.max() <= 255
            ), f"Frame value out of range: {frame.min()} ~ {frame.max()}"
        else:
            frame = images[i]
        frames.append(frame)

    frames = np.stack(frames)
    iio.imwrite(
        output_path,
        frames,
        fps=fps,
        codec="libx264",
        pixelformat="yuv420p",
        bitrate=bitrate,
        macro_block_size=16,
    )