]> git.sesse.net Git - nageru/blobdiff - futatabi/export.cpp
Make the MIDI play button blinking when something is ready to play, and solid when...
[nageru] / futatabi / export.cpp
index bed2643547a03335662bbda0f60dec36cd5fa981..50fc950bbd2fa8ead59a365c28375b9be66ac278 100644 (file)
@@ -1,17 +1,18 @@
+#include "export.h"
+
 #include "clip_list.h"
 #include "defs.h"
-#include "export.h"
 #include "flags.h"
 #include "frame_on_disk.h"
+#include "player.h"
 #include "shared/ffmpeg_raii.h"
 #include "shared/timebase.h"
 
 #include <QMessageBox>
 #include <QProgressDialog>
-
-#include <vector>
-
+#include <future>
 #include <unistd.h>
+#include <vector>
 
 extern "C" {
 #include <libavformat/avformat.h>
@@ -98,14 +99,14 @@ void export_multitrack_clip(const string &filename, const Clip &clip)
 
        // Create the streams. Note that some of them could be without frames
        // (we try to maintain the stream indexes in the export).
-       vector<AVStream *> video_streams; 
+       vector<AVStream *> video_streams;
        for (unsigned stream_idx = 0; stream_idx <= last_stream_idx; ++stream_idx) {
                AVStream *avstream_video = avformat_new_stream(avctx, nullptr);
                if (avstream_video == nullptr) {
                        fprintf(stderr, "avformat_new_stream() failed\n");
                        exit(1);
                }
-               avstream_video->time_base = AVRational{1, TIMEBASE};
+               avstream_video->time_base = AVRational{ 1, TIMEBASE };
                avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
                avstream_video->codecpar->codec_id = AV_CODEC_ID_MJPEG;
                avstream_video->codecpar->width = global_flags.width;  // Might be wrong, but doesn't matter all that much.
@@ -163,8 +164,8 @@ void export_multitrack_clip(const string &filename, const Clip &clip)
                        }
                }
                string jpeg = readers[first_frame_stream_idx].read_frame(first_frame);
-               int64_t scaled_pts = av_rescale_q(first_frame.pts, AVRational{1, TIMEBASE},
-                       video_streams[first_frame_stream_idx]->time_base);
+               int64_t scaled_pts = av_rescale_q(first_frame.pts, AVRational{ 1, TIMEBASE },
+                                                 video_streams[first_frame_stream_idx]->time_base);
                buffered_jpegs.emplace_back(BufferedJPEG{ scaled_pts, first_frame_stream_idx, std::move(jpeg) });
                if (buffered_jpegs.size() >= 1000) {
                        if (!write_buffered_jpegs(avctx, buffered_jpegs)) {
@@ -181,7 +182,7 @@ void export_multitrack_clip(const string &filename, const Clip &clip)
                if (progress.wasCanceled()) {
                        unlink(filename.c_str());
                        return;
-                }
+               }
        }
 
        if (!write_buffered_jpegs(avctx, buffered_jpegs)) {
@@ -194,3 +195,57 @@ void export_multitrack_clip(const string &filename, const Clip &clip)
        frames_written += buffered_jpegs.size();
        progress.setValue(frames_written);
 }
+
+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([&current_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.
+       }
+}