Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca-ffmpeg-sink.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42#ifndef CA_FFMPEG_SINK_H
43#define CA_FFMPEG_SINK_H
44
45#include <cstdio>
46#include <cstdlib>
47#include <filesystem>
48#include <sstream>
49#include <string>
50#include <utility>
51
52#include <ah-errors.H>
53
54#include <ca-io.H>
55
56namespace Aleph {
57namespace CA {
58
61{
62 unsigned fps = 24;
63 std::string codec = "libx264";
64 std::string pixel_format = "yuv420p";
65 bool auto_start = true;
66 bool require_ffmpeg = false;
67};
68
69namespace ca_ffmpeg_detail {
70
71[[nodiscard]] inline std::string shell_quote(const std::string &s)
72{
73 std::string out = "'";
74 for (const char c : s)
75 {
76 if (c == '\'')
77 out += "'\\''";
78 else
79 out.push_back(c);
80 }
81 out.push_back('\'');
82 return out;
83}
84
85[[nodiscard]] inline bool ffmpeg_available()
86{
87 return std::system("ffmpeg -version >/dev/null 2>&1") == 0;
88}
89
90} // namespace ca_ffmpeg_detail
91
96template <typename Mapper>
98{
99 std::filesystem::path output_;
102 std::string command_;
103 FILE *pipe_ = nullptr;
104
105 void start()
106 {
107 command_ = "ffmpeg -y -loglevel error -f image2pipe -vcodec ppm -r " + std::to_string(opts_.fps)
108 + " -i - -an -vcodec " + ca_ffmpeg_detail::shell_quote(opts_.codec) + " -pix_fmt "
111 if (not opts_.auto_start)
112 return;
114 {
116 << "Ffmpeg_Frame_Sink: ffmpeg is not available in PATH";
117 return;
118 }
119 pipe_ = popen(command_.c_str(), "w");
121 << "Ffmpeg_Frame_Sink: cannot start ffmpeg";
122 }
123
124public:
131 : output_(std::move(output)), mapper_(std::move(mapper)), opts_(std::move(opts))
132 {
133 start();
134 }
135
138
143 : output_(std::move(other.output_)), mapper_(std::move(other.mapper_)),
144 opts_(std::move(other.opts_)), command_(std::move(other.command_)), pipe_(other.pipe_)
145 {
146 other.pipe_ = nullptr;
147 }
148
151 {
152 if (pipe_ != nullptr)
153 pclose(pipe_);
154 }
155
159 [[nodiscard]] const std::string &command() const noexcept
160 {
161 return command_;
162 }
163
168 {
169 return pipe_ != nullptr;
170 }
171
177 template <typename Lattice>
178 void accept(const std::size_t step, const Lattice &frame)
179 {
180 (void) step;
181 if (pipe_ == nullptr)
182 return;
183 std::ostringstream ppm(std::ios::binary);
184 write_ppm(ppm, frame, mapper_);
185 const std::string bytes = ppm.str();
186 const std::size_t written = std::fwrite(bytes.data(), 1, bytes.size(), pipe_);
187 ah_runtime_error_if(written != bytes.size())
188 << "Ffmpeg_Frame_Sink::accept: ffmpeg pipe write failed";
189 }
190
192 void flush()
193 {
194 if (pipe_ == nullptr)
195 return;
196 std::fflush(pipe_);
197 const int status = pclose(pipe_);
198 pipe_ = nullptr;
200 << "Ffmpeg_Frame_Sink::flush: ffmpeg exited with status " << status;
201 }
202};
203
204template <typename Mapper>
206
207} // namespace CA
208} // namespace Aleph
209
210#endif // CA_FFMPEG_SINK_H
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:271
size_t size_t int32_t * out
Definition ca-c-api.h:120
File-format readers and writers for cellular-automata frames.
Frame sink that streams PPM frames into ffmpeg.
bool active() const noexcept
Return true when a live ffmpeg process is attached.
Ffmpeg_Frame_Sink(Ffmpeg_Frame_Sink &&other) noexcept
Move-construct a sink, transferring the process handle.
Ffmpeg_Frame_Sink(std::filesystem::path output, Mapper mapper, Ffmpeg_Options opts={})
Build an ffmpeg frame sink.
~Ffmpeg_Frame_Sink()
Close ffmpeg best-effort.
const std::string & command() const noexcept
Return the ffmpeg command used by this sink.
void flush()
Flush and close the ffmpeg process if one was started.
void accept(const std::size_t step, const Lattice &frame)
Send one frame to ffmpeg, or no-op when inactive.
Ffmpeg_Frame_Sink(const Ffmpeg_Frame_Sink &)=delete
Ffmpeg_Frame_Sink & operator=(const Ffmpeg_Frame_Sink &)=delete
std::filesystem::path output_
Lattice that adds boundary-aware access on top of a storage.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::string shell_quote(const std::string &s)
void write_ppm(std::ostream &out, const Lattice &frame, Mapper &&mapper, const NetPBM_Write_Options &opts={})
Write a binary PPM (P6) image.
Definition ca-io.H:1496
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Options for Ffmpeg_Frame_Sink.
bool auto_start
start ffmpeg in the constructor
std::string pixel_format
output pixel format
std::string codec
ffmpeg encoder name
unsigned fps
input and output frame rate
bool require_ffmpeg
throw if ffmpeg cannot be started
ofstream output
Definition writeHeap.C:215