-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmpl-writer.html
More file actions
174 lines (145 loc) · 11.8 KB
/
mpl-writer.html
File metadata and controls
174 lines (145 loc) · 11.8 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
<meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Creating Videos from Matplotlib figure — python-ffmpegio 0.11.0 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=80d5e7a1" />
<link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=e59714d7" />
<link rel="stylesheet" type="text/css" href="_static/plot_directive.css" />
<link rel="stylesheet" type="text/css" href="_static/css/custom.css?v=048f17f8" />
<script src="_static/jquery.js?v=5d32c60e"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="_static/documentation_options.js?v=f3b36f1a"></script>
<script src="_static/doctools.js?v=9bcbadda"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/js/theme.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="index.html" class="icon icon-home">
python-ffmpegio
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<!-- Local TOC -->
<div class="local-toc"><ul>
<li><a class="reference internal" href="#">Creating Videos from Matplotlib figure</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">python-ffmpegio</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item active">Creating Videos from Matplotlib figure</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/mpl-writer.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="creating-videos-from-matplotlib-figure">
<span id="options"></span><h1>Creating Videos from Matplotlib figure<a class="headerlink" href="#creating-videos-from-matplotlib-figure" title="Link to this heading"></a></h1>
<p>While Matplotlib supports video creation via its
<a class="reference external" href="https://matplotlib.org/stable/users/explain/animations/animations.html">animation module</a>,
its interface is a bit cranky because its primary role is to animate the figure on screen
rather than outputting figures to a video file. You must create an animation object first before
saving it as a video.</p>
<p><code class="code docutils literal notranslate"><span class="pre">ffmpegio</span></code> provides a direct method to write Matplotlib figure to a video write stream with
the same streaming interface as feeding the RGB frame data to FFmpeg.</p>
<section id="example">
<h2>Example<a class="headerlink" href="#example" title="Link to this heading"></a></h2>
<p>Create an MP4 video of <a class="reference external" href="https://matplotlib.org/stable/gallery/animation/simple_anim.html">Matplotlib’s animation example</a>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">ffmpegio</span> <span class="k">as</span> <span class="nn">ff</span>
<span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
<span class="n">line</span><span class="p">,</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="n">interval</span><span class="o">=</span><span class="mi">20</span> <span class="c1"># delay in milliseconds</span>
<span class="n">save_count</span><span class="o">=</span><span class="mi">50</span> <span class="c1"># number of frames</span>
<span class="k">def</span> <span class="nf">animate</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="n">line</span><span class="o">.</span><span class="n">set_ydata</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="n">i</span> <span class="o">/</span> <span class="mi">50</span><span class="p">))</span> <span class="c1"># update the data.</span>
<span class="k">return</span> <span class="n">line</span>
<span class="k">with</span> <span class="n">ff</span><span class="o">.</span><span class="n">open</span><span class="p">(</span>
<span class="s2">"output.mp4"</span><span class="p">,</span> <span class="c1"># output file name</span>
<span class="s2">"wv"</span><span class="p">,</span> <span class="c1"># open file in write-video mode</span>
<span class="mf">1e3</span><span class="o">/</span><span class="n">interval</span><span class="p">,</span> <span class="c1"># framerate in frames/second</span>
<span class="n">pix_fmt</span><span class="o">=</span><span class="s2">"yuv420p"</span><span class="p">,</span> <span class="c1"># specify the pixel format (default is yuv444p)</span>
<span class="c1"># add other ffmpeg options as keywod argument as needed</span>
<span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">save_count</span><span class="p">):</span>
<span class="n">animate</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="c1"># update figure</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">fig</span><span class="p">)</span> <span class="c1"># write new video frame</span>
</pre></div>
</div>
<p>Any video format can be chosen with this interface and any FFmpeg options can be specified here.
For instance, an GIF animation of the above example can be created with optimized color pallette.
To do this, we use <a class="reference external" href="https://ffmpeg.org/ffmpeg-filters.html#palettegen">palettegen</a> and
<cite>paletteuse <https://ffmpeg.org/ffmpeg-filters.html#paletteuse>`__</cite> filters and construct a video filtergraph:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">split</span><span class="p">[</span><span class="n">s0</span><span class="p">][</span><span class="n">s1</span><span class="p">];[</span><span class="n">s0</span><span class="p">]</span><span class="n">palettegen</span><span class="p">[</span><span class="n">p</span><span class="p">];[</span><span class="n">s1</span><span class="p">][</span><span class="n">p</span><span class="p">]</span><span class="n">paletteuse</span>
</pre></div>
</div>
<p>This filtergraph string could be provided directly to <code class="code docutils literal notranslate"><span class="pre">ff.open</span></code> as a <cite>vf</cite> keyword argument,
but let’s use <code class="code docutils literal notranslate"><span class="pre">ffmpegio.filtergraph</span></code> submodule to construct it instead:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">ffmpegio.filtergraph</span> <span class="k">as</span> <span class="nn">fgb</span>
<span class="n">vf</span> <span class="o">=</span> <span class="n">fgb</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="o">+</span> <span class="n">fgb</span><span class="o">.</span><span class="n">palettegen</span><span class="p">()</span> <span class="o">+</span> <span class="n">fgb</span><span class="o">.</span><span class="n">paletteuse</span><span class="p">()</span>
<span class="k">with</span> <span class="n">ff</span><span class="o">.</span><span class="n">open</span><span class="p">(</span>
<span class="s2">"output.gif"</span><span class="p">,</span> <span class="c1"># output file name</span>
<span class="s2">"wv"</span><span class="p">,</span> <span class="c1"># open file in write-video mode</span>
<span class="mf">1e3</span><span class="o">/</span><span class="n">interval</span><span class="p">,</span> <span class="c1"># framerate in frames/second</span>
<span class="n">vf</span> <span class="o">=</span> <span class="n">vf</span> <span class="c1"># optimize the GIF palette</span>
<span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">save_count</span><span class="p">):</span>
<span class="n">animate</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="c1"># update figure</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">fig</span><span class="p">)</span> <span class="c1"># write new video frame</span>
</pre></div>
</div>
</section>
</section>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>© Copyright 2021-2022, Takeshi (Kesh) Ikuma, Louisiana State University Health Sciences Center.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>