Lab 2: 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 7.1 Copyright (c) 2000-2024 the FFmpeg developers
built with gcc 13.3.0 (conda-forge gcc 13.3.0-2)
configuration: --prefix=/home/runner/miniconda3/envs/phys142 --cc=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/x86_64-conda-linux-gnu-cc --cxx=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/x86_64-conda-linux-gnu-c++ --nm=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/x86_64-conda-linux-gnu-nm --ar=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/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-libmp3lame --enable-libvpx --enable-libass --enable-pthreads --enable-alsa --enable-libpulse --enable-vaapi --enable-libopenvino --enable-gpl --enable-libx264 --enable-libx265 --enable-libaom --enable-libsvtav1 --enable-libxml2 --enable-pic --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libvorbis --enable-libopus --enable-librsvg --enable-ffplay --pkg-config=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/pkg-config
libavutil 59. 39.100 / 59. 39.100
libavcodec 61. 19.100 / 61. 19.100
libavformat 61. 7.100 / 61. 7.100
libavdevice 61. 3.100 / 61. 3.100
libavfilter 10. 4.100 / 10. 4.100
libswscale 8. 3.100 / 8. 3.100
libswresample 5. 3.100 / 5. 3.100
libpostproc 58. 3.100 / 58. 3.100
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 @ 0x563e91c51880] using SAR=1/1
[libx264 @ 0x563e91c51880] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x563e91c51880] profile High 4:4:4 Predictive, level 3.0, 4:4:4, 8-bit
[libx264 @ 0x563e91c51880] 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 : Lavf61.7.100
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 : Lavc61.19.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[out#0/mp4 @ 0x563e91c52600] 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=11.6x
[libx264 @ 0x563e91c51880] frame I:1 Avg QP:17.61 size: 5897
[libx264 @ 0x563e91c51880] frame P:50 Avg QP:15.10 size: 391
[libx264 @ 0x563e91c51880] frame B:49 Avg QP:18.92 size: 199
[libx264 @ 0x563e91c51880] consecutive B-frames: 22.0% 34.0% 12.0% 32.0%
[libx264 @ 0x563e91c51880] mb I I16..4: 30.1% 57.6% 12.3%
[libx264 @ 0x563e91c51880] 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 @ 0x563e91c51880] 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 @ 0x563e91c51880] 8x8 transform intra:54.4% inter:37.3%
[libx264 @ 0x563e91c51880] coded y,u,v intra: 3.8% 1.6% 1.8% inter: 0.1% 0.0% 0.0%
[libx264 @ 0x563e91c51880] i16 v,h,dc,p: 67% 27% 6% 0%
[libx264 @ 0x563e91c51880] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 4% 69% 0% 0% 0% 0% 0% 0%
[libx264 @ 0x563e91c51880] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 22% 33% 5% 2% 5% 1% 7% 1%
[libx264 @ 0x563e91c51880] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x563e91c51880] ref P L0: 62.2% 0.8% 23.9% 13.1%
[libx264 @ 0x563e91c51880] ref B L0: 76.7% 20.0% 3.3%
[libx264 @ 0x563e91c51880] ref B L1: 96.2% 3.8%
[libx264 @ 0x563e91c51880] 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 7.1 Copyright (c) 2000-2024 the FFmpeg developers
built with gcc 13.3.0 (conda-forge gcc 13.3.0-2)
configuration: --prefix=/home/runner/miniconda3/envs/phys142 --cc=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/x86_64-conda-linux-gnu-cc --cxx=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/x86_64-conda-linux-gnu-c++ --nm=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/x86_64-conda-linux-gnu-nm --ar=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/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-libmp3lame --enable-libvpx --enable-libass --enable-pthreads --enable-alsa --enable-libpulse --enable-vaapi --enable-libopenvino --enable-gpl --enable-libx264 --enable-libx265 --enable-libaom --enable-libsvtav1 --enable-libxml2 --enable-pic --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libvorbis --enable-libopus --enable-librsvg --enable-ffplay --pkg-config=/home/conda/feedstock_root/build_artifacts/ffmpeg_1740542346949/_build_env/bin/pkg-config
libavutil 59. 39.100 / 59. 39.100
libavcodec 61. 19.100 / 61. 19.100
libavformat 61. 7.100 / 61. 7.100
libavdevice 61. 3.100 / 61. 3.100
libavfilter 10. 4.100 / 10. 4.100
libswscale 8. 3.100 / 8. 3.100
libswresample 5. 3.100 / 5. 3.100
libpostproc 58. 3.100 / 58. 3.100
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 @ 0x5636a5c78800] using SAR=1/1
[libx264 @ 0x5636a5c78800] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x5636a5c78800] profile High 4:4:4 Predictive, level 3.0, 4:4:4, 8-bit
[libx264 @ 0x5636a5c78800] 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 : Lavf61.7.100
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 : Lavc61.19.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
[out#0/mp4 @ 0x5636a5c79580] 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.1x
[libx264 @ 0x5636a5c78800] frame I:1 Avg QP:17.61 size: 5897
[libx264 @ 0x5636a5c78800] frame P:50 Avg QP:15.10 size: 391
[libx264 @ 0x5636a5c78800] frame B:49 Avg QP:18.92 size: 199
[libx264 @ 0x5636a5c78800] consecutive B-frames: 22.0% 34.0% 12.0% 32.0%
[libx264 @ 0x5636a5c78800] mb I I16..4: 30.1% 57.6% 12.3%
[libx264 @ 0x5636a5c78800] 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 @ 0x5636a5c78800] 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 @ 0x5636a5c78800] 8x8 transform intra:54.4% inter:37.3%
[libx264 @ 0x5636a5c78800] coded y,u,v intra: 3.8% 1.6% 1.8% inter: 0.1% 0.0% 0.0%
[libx264 @ 0x5636a5c78800] i16 v,h,dc,p: 67% 27% 6% 0%
[libx264 @ 0x5636a5c78800] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 4% 69% 0% 0% 0% 0% 0% 0%
[libx264 @ 0x5636a5c78800] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 22% 33% 5% 2% 5% 1% 7% 1%
[libx264 @ 0x5636a5c78800] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x5636a5c78800] ref P L0: 62.2% 0.8% 23.9% 13.1%
[libx264 @ 0x5636a5c78800] ref B L0: 76.7% 20.0% 3.3%
[libx264 @ 0x5636a5c78800] ref B L1: 96.2% 3.8%
[libx264 @ 0x5636a5c78800] 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.