Lab 02: ffmpeg

Lab 02: ffmpeg#

Links:

Make Plots#

import matplotlib.pyplot as plt
import numpy as np

We’ll store our plots in folder build. If it does not exist, create it.

# if folder build does not exist, create it
import os
if not os.path.exists('build'):
    os.makedirs('build')

We can make animations by stacking images together. First let’s make some plots.

As an example, we’ll make a plot of a 1D wave

\[y = \sin(x + t)\]

over a range of \(-\pi < x < \pi\) and \(0 < t < 2\pi\).

# make plots of y = sin(x + t) for -pi < x < pi and 0 < t < 2pi, save as png
x = np.linspace(-np.pi, np.pi, 100)
for i, t in enumerate(np.linspace(0, 2*np.pi, 100)):
    y = np.sin(x + t)
    # draw the plot on canvas
    plt.plot(x, y)
    # save the plot in the format demo_XX.png, e.g. demo_00.png, demo_01.png, ...
    plt.savefig('build/demo_{:02d}.png'.format(i))
    # clear the canvas
    plt.clf()
    
<Figure size 640x480 with 0 Axes>

Create a Video Using ffmpeg#

A minimal example of using ffmpeg to create a video is

ffmpeg -i build/demo_%02d.png -r 25 build/demo.mp4

where -i specifies the input file names, build/%02d.png represents all files in the format demo_00.png, demo_01.png, ... in the build folder, -r 25 sets the frame rate to 25fps, and build/demo.mp4 sets the output file name.

# create a video from the png files
os.system('ffmpeg -r 25 -i build/demo_%02d.png build/demo.mp4 -y')
ffmpeg version 9.0.2 Copyright (c) 2000-2026 the FFmpeg developers
  built with gcc 15.3.0 (conda-forge gcc 15.3.0-5)
  configuration: --prefix=/home/runner/miniconda3/envs/phys142 --cc=x86_64-conda-linux-gnu-cc --cxx=x86_64-conda-linux-gnu-c++ --nm=x86_64-conda-linux-gnu-nm --ar=x86_64-conda-linux-gnu-ar --disable-doc --enable-openssl --enable-demuxer=dash --enable-hardcoded-tables --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig --enable-libopenh264 --enable-libdav1d --disable-gnutls --enable-libvpx --enable-libass --enable-libplacebo --enable-pthreads --enable-alsa --enable-libpulse --enable-libvpl --enable-vaapi --enable-libopenvino --enable-gpl --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-libaom --enable-libsvtav1 --enable-libxml2 --enable-pic --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libvorbis --enable-libopus --enable-librsvg --enable-libjxl --enable-libwebp --enable-ffplay --enable-vulkan --pkg-config=/home/conda/feedstock_root/build_artifacts/ffmpeg_1790246669743/_build_env/bin/pkg-config
  libavutil      61.  1.102 / 61.  1.102
  libavcodec     63.  1.102 / 63.  1.102
  libavformat    63.  1.102 / 63.  1.102
  libavdevice    63.  1.102 / 63.  1.102
  libavfilter    12.  1.102 / 12.  1.102
  libswscale     10.  1.102 / 10.  1.102
  libswresample   7.  1.102 /  7.  1.102
Input #0, image2, from 'build/demo_%02d.png':
  Duration: 00:00:04.00, start: 0.000000, bitrate: N/A
  Stream #0:0: Video: png, rgba(pc, gbr/unknown/unknown), 640x480 [SAR 3937:3937 DAR 4:3], 25 fps, 25 tbr, 25 tbn
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x56364c76dc40] using SAR=1/1
[libx264 @ 0x56364c76dc40] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 AVX512
[libx264 @ 0x56364c76dc40] profile High 4:4:4 Predictive, level 3.0, 4:4:4, 8-bit
[libx264 @ 0x56364c76dc40] 264 - core 164 r3095 baee400 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'build/demo.mp4':
  Metadata:
    encoder         : Lavf63.1.102
  Stream #0:0: Video: h264 (avc1 / 0x31637661), yuv444p(tv, progressive), 640x480 [SAR 1:1 DAR 4:3], q=2-31, 25 fps, 12800 tbn
    Metadata:
      encoder         : Lavc63.1.102 libx264
    Side data:
      CPB properties: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[out#0/mp4 @ 0x56364c83c540] video:35KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 5.403522%
frame=  100 fps=0.0 q=-1.0 Lsize=      37KiB time=00:00:03.92 bitrate=  77.2kbits/s speed=12.7x elapsed=0:00:00.30    
[libx264 @ 0x56364c76dc40] frame I:1     Avg QP:17.61  size:  5897
[libx264 @ 0x56364c76dc40] frame P:50    Avg QP:15.10  size:   391
[libx264 @ 0x56364c76dc40] frame B:49    Avg QP:18.92  size:   199
[libx264 @ 0x56364c76dc40] consecutive B-frames: 22.0% 34.0% 12.0% 32.0%
[libx264 @ 0x56364c76dc40] mb I  I16..4: 30.1% 57.6% 12.3%
[libx264 @ 0x56364c76dc40] mb P  I16..4:  1.1%  1.2%  0.1%  P16..4:  5.2%  1.6%  0.3%  0.0%  0.0%    skip:90.5%
[libx264 @ 0x56364c76dc40] mb B  I16..4:  0.0%  0.1%  0.0%  B16..8:  7.9%  1.4%  0.0%  direct: 0.0%  skip:90.6%  L0:50.5% L1:45.2% BI: 4.2%
[libx264 @ 0x56364c76dc40] 8x8 transform intra:54.4% inter:37.3%
[libx264 @ 0x56364c76dc40] coded y,u,v intra: 3.8% 1.6% 1.8% inter: 0.1% 0.0% 0.0%
[libx264 @ 0x56364c76dc40] i16 v,h,dc,p: 67% 27%  6%  0%
[libx264 @ 0x56364c76dc40] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27%  4% 69%  0%  0%  0%  0%  0%  0%
[libx264 @ 0x56364c76dc40] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 22% 33%  5%  2%  5%  1%  7%  1%
[libx264 @ 0x56364c76dc40] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x56364c76dc40] ref P L0: 62.2%  0.8% 23.9% 13.1%
[libx264 @ 0x56364c76dc40] ref B L0: 76.7% 20.0%  3.3%
[libx264 @ 0x56364c76dc40] ref B L1: 96.2%  3.8%
[libx264 @ 0x56364c76dc40] kb/s:70.39
0

You can also run commands in a notebook using !:

!ffmpeg -i build/demo_%02d.png -r 25 build/demo.mp4

Either way works. You can also directly run the command in the terminal.

!ffmpeg -i build/demo_%02d.png -r 25 build/demo.mp4 -y
ffmpeg version 9.0.2 Copyright (c) 2000-2026 the FFmpeg developers
  built with gcc 15.3.0 (conda-forge gcc 15.3.0-5)
  configuration: --prefix=/home/runner/miniconda3/envs/phys142 --cc=x86_64-conda-linux-gnu-cc --cxx=x86_64-conda-linux-gnu-c++ --nm=x86_64-conda-linux-gnu-nm --ar=x86_64-conda-linux-gnu-ar --disable-doc --enable-openssl --enable-demuxer=dash --enable-hardcoded-tables --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig --enable-libopenh264 --enable-libdav1d --disable-gnutls --enable-libvpx --enable-libass --enable-libplacebo --enable-pthreads --enable-alsa --enable-libpulse --enable-libvpl --enable-vaapi --enable-libopenvino --enable-gpl --enable-libx264 --enable-libx265 --enable-libmp3lame --enable-libaom --enable-libsvtav1 --enable-libxml2 --enable-pic --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libvorbis --enable-libopus --enable-librsvg --enable-libjxl --enable-libwebp --enable-ffplay --enable-vulkan --pkg-config=/home/conda/feedstock_root/build_artifacts/ffmpeg_1790246669743/_build_env/bin/pkg-config
  libavutil      61.  1.102 / 61.  1.102
  libavcodec     63.  1.102 / 63.  1.102
  libavformat    63.  1.102 / 63.  1.102
  libavdevice    63.  1.102 / 63.  1.102
  libavfilter    12.  1.102 / 12.  1.102
  libswscale     10.  1.102 / 10.  1.102
  libswresample   7.  1.102 /  7.  1.102
Input #0, image2, from 'build/demo_%02d.png':
  Duration: 00:00:04.00, start: 0.000000, bitrate: N/A
  Stream #0:0: Video: png, rgba(pc, gbr/unknown/unknown), 640x480 [SAR 3937:3937 DAR 4:3], 25 fps, 25 tbr, 25 tbn
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x559ee795dbc0] using SAR=1/1
[libx264 @ 0x559ee795dbc0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 AVX512
[libx264 @ 0x559ee795dbc0] profile High 4:4:4 Predictive, level 3.0, 4:4:4, 8-bit
[libx264 @ 0x559ee795dbc0] 264 - core 164 r3095 baee400 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'build/demo.mp4':
  Metadata:
    encoder         : Lavf63.1.102
  Stream #0:0: Video: h264 (avc1 / 0x31637661), yuv444p(tv, progressive), 640x480 [SAR 1:1 DAR 4:3], q=2-31, 25 fps, 12800 tbn
    Metadata:
      encoder         : Lavc63.1.102 libx264
    Side data:
      CPB properties: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[out#0/mp4 @ 0x559ee7a2c4c0] video:35KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 5.403522%
frame=  100 fps=0.0 q=-1.0 Lsize=      37KiB time=00:00:03.92 bitrate=  77.2kbits/s speed=  13x elapsed=0:00:00.30    
[libx264 @ 0x559ee795dbc0] frame I:1     Avg QP:17.61  size:  5897
[libx264 @ 0x559ee795dbc0] frame P:50    Avg QP:15.10  size:   391
[libx264 @ 0x559ee795dbc0] frame B:49    Avg QP:18.92  size:   199
[libx264 @ 0x559ee795dbc0] consecutive B-frames: 22.0% 34.0% 12.0% 32.0%
[libx264 @ 0x559ee795dbc0] mb I  I16..4: 30.1% 57.6% 12.3%
[libx264 @ 0x559ee795dbc0] mb P  I16..4:  1.1%  1.2%  0.1%  P16..4:  5.2%  1.6%  0.3%  0.0%  0.0%    skip:90.5%
[libx264 @ 0x559ee795dbc0] mb B  I16..4:  0.0%  0.1%  0.0%  B16..8:  7.9%  1.4%  0.0%  direct: 0.0%  skip:90.6%  L0:50.5% L1:45.2% BI: 4.2%
[libx264 @ 0x559ee795dbc0] 8x8 transform intra:54.4% inter:37.3%
[libx264 @ 0x559ee795dbc0] coded y,u,v intra: 3.8% 1.6% 1.8% inter: 0.1% 0.0% 0.0%
[libx264 @ 0x559ee795dbc0] i16 v,h,dc,p: 67% 27%  6%  0%
[libx264 @ 0x559ee795dbc0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27%  4% 69%  0%  0%  0%  0%  0%  0%
[libx264 @ 0x559ee795dbc0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 22% 33%  5%  2%  5%  1%  7%  1%
[libx264 @ 0x559ee795dbc0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x559ee795dbc0] ref P L0: 62.2%  0.8% 23.9% 13.1%
[libx264 @ 0x559ee795dbc0] ref B L0: 76.7% 20.0%  3.3%
[libx264 @ 0x559ee795dbc0] ref B L1: 96.2%  3.8%
[libx264 @ 0x559ee795dbc0] kb/s:70.39

Watch the video

# watch the video
import IPython.display as ipd
ipd.Video('build/demo.mp4')

Congratulations! You’ve made your first video using ffmpeg.