fstark / macflim

MacFlim flim player source code and utilities
MIT License
90 stars 4 forks source link

Support creation of longer flims #5

Open fstark opened 3 years ago

fstark commented 3 years ago

Creating flims longer than 10 minutes is impractical, due to the memory consumption of flimmaker.

fstark commented 2 years ago

From this reddit comment: https://www.reddit.com/r/cpp/comments/s980ik/a_highlevel_coroutine_explanation/htsa1m4/

Here is an example of how to implement coroutines for video/audio: https://godbolt.org/z/Y7aEP9aoo

Using the generator from: https://github.com/lewissbaker/cppcoro

Copy of the code:

#include <cppcoro/generator.hpp>
#include <iostream>
#include <random>
#include <tuple>
#include <vector>

using video_frame = int;
using audio_frame = double;
using data_packet = std::tuple<std::vector<video_frame>, audio_frame>;

cppcoro::generator<data_packet> decoder() {
    std::random_device r;
    std::default_random_engine engine{r()};
    std::uniform_int_distribution<video_frame> uniform_dist(-100, 100);

    std::vector<video_frame> buffer{};

    for (std::size_t iter{1}; iter < 100; ++iter) {
        if (iter % 10 != 0) {
            buffer.push_back(uniform_dist(engine));
        } else {
            co_yield data_packet{buffer, 10.6 * iter};
            buffer.clear();
        }
    }
}

int main() {
    for (auto const& [video_buffer, audio_buffer] : decoder()) {
        std::cout << "Audio " << audio_buffer << "\nVideo ";

        for (auto frame : video_buffer) {
            std::cout << frame << ",";
        }

        std::cout << "\n";
    }
    return 0;
}