+
+void export_interpolated_clip(const string &filename, const vector<Clip> &clips)
+{
+ AVFormatContext *avctx = nullptr;
+ avformat_alloc_output_context2(&avctx, NULL, NULL, filename.c_str());
+ if (avctx == nullptr) {
+ QMessageBox msgbox;
+ msgbox.setText("Could not allocate FFmpeg context");
+ msgbox.exec();
+ return;
+ }
+ AVFormatContextWithCloser closer(avctx);
+
+ int ret = avio_open(&avctx->pb, filename.c_str(), AVIO_FLAG_WRITE);
+ if (ret < 0) {
+ QMessageBox msgbox;
+ msgbox.setText(QString::fromStdString("Could not open output file '" + filename + "'"));
+ msgbox.exec();
+ return;
+ }
+
+ QProgressDialog progress(QString::fromStdString("Exporting to " + filename + "..."), "Abort", 0, 1);
+ progress.setWindowTitle("Futatabi");
+ progress.setWindowModality(Qt::WindowModal);
+ progress.setMinimumDuration(1000);
+ progress.setMaximum(100000);
+ progress.setValue(0);
+
+ vector<ClipWithID> clips_with_id;
+ for (const Clip &clip : clips) {
+ clips_with_id.emplace_back(ClipWithID{ clip, 0 });
+ }
+ double total_length = compute_total_time(clips_with_id);
+
+ promise<void> done_promise;
+ future<void> done = done_promise.get_future();
+ std::atomic<double> current_value{ 0.0 };
+
+ Player player(/*destination=*/nullptr, Player::FILE_STREAM_OUTPUT, closer.release());
+ player.set_done_callback([&done_promise] {
+ done_promise.set_value();
+ });
+ player.set_progress_callback([¤t_value, &clips, total_length](const std::map<uint64_t, double> &player_progress, double time_remaining) {
+ current_value = 1.0 - time_remaining / total_length;
+ });
+ player.play(clips_with_id);
+ while (done.wait_for(std::chrono::milliseconds(100)) != future_status::ready && !progress.wasCanceled()) {
+ progress.setValue(lrint(100000.0 * current_value));
+ }
+ if (progress.wasCanceled()) {
+ unlink(filename.c_str());
+ // Destroying player on scope exit will abort the render job.
+ }
+}