File size: 1,305 Bytes
cbcb207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import shutil


def create_video_from_intermediate_results(results_path):
    #
    # change this depending on what you want to accomplish (modify out video
    # name, change fps and trim video)
    #
    img_format = (4, '.jpg')
    out_file_name = 'out.mp4'
    fps = 10
    first_frame = 0
    number_of_frames_to_process = len(os.listdir(results_path))
    ffmpeg = 'ffmpeg'
    if shutil.which(ffmpeg):  # if ffmpeg is in system path
        # example: '%4d.png' for (4, '.png')
        img_name_format = '%' + str(img_format[0]) + 'd' + img_format[1]
        pattern = os.path.join(results_path, img_name_format)
        out_video_path = os.path.join(results_path, out_file_name)

        trim_video_command = [
            '-start_number',
            str(first_frame), '-vframes',
            str(number_of_frames_to_process)
        ]
        input_options = ['-r', str(fps), '-i', pattern]
        encoding_options = [
            '-c:v', 'libx264', '-crf', '25', '-pix_fmt', 'yuv420p', 
            '-vf', "pad=ceil(iw/2)*2:ceil(ih/2)*2"
        ]
        subprocess.call([
            ffmpeg, *input_options, *trim_video_command, *encoding_options,
            out_video_path
        ])
    else:
        print(f'{ffmpeg} not found in the system path, aborting.')