File: extract-frames-post-processor.vala

package info (click to toggle)
peek 1.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,768 kB
  • sloc: xml: 234; sh: 85; python: 69; makefile: 14
file content (80 lines) | stat: -rw-r--r-- 2,348 bytes parent folder | download | duplicates (5)
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
/*
Peek Copyright (c) 2017 by Philipp Wolfer <ph.wolfer@gmail.com>

This file is part of Peek.

This software is licensed under the GNU General Public License
(version 3 or later). See the LICENSE file in this distribution.
*/

namespace Peek.PostProcessing {

  /**
  * Uses ffmpeg to generate PNG images for each frame.
  */
  public class ExtractFramesPostProcessor : CliPostProcessor {
    private string executable = null;
    private static Posix.Glob glob = Posix.Glob ();

    public override async Array<File>? process_async (Array<File> files) throws RecordingError {
      var input_file = files.index (0);
      string[] args = {
        find_executable (), "-y",
        "-i", input_file.get_path (),
        get_png_filename_pattern (input_file, "%04d")
      };

      try {
        yield spawn_command_async (args);
      } catch (RecordingError e) {
        yield remove_output_files (input_file);
        throw e;
      }

      var output = get_png_output_files (input_file);
      return output;
    }

    private static string get_png_filename_pattern (File input_file, string replacement) {
      return input_file.get_path () + "." + replacement + ".png";
    }

    public static Array<File> get_png_output_files (File input_file) {
      var png_file_pattern = get_png_filename_pattern (input_file, "*");
      glob.glob (png_file_pattern);
      var output = new Array<File> ();
      foreach (string png_file_path in glob.pathv) {
        var file = File.new_for_path (png_file_path);
        output.append_val (file);
      }

      return output;
    }

    private static async void remove_output_files (File input_file) {
      var output = get_png_output_files (input_file);
      foreach (File file in output.data) {
        try {
          yield file.delete_async ();
        } catch (Error e) {
          stderr.printf ("Error deleting temporary file %s: %s\n", file.get_path (), e.message);
        }
      }
    }

    private string find_executable () {
      if (executable == null) {
        string[] tools = { "ffmpeg", "avconv" };
        foreach (string tool in tools) {
          if (Utils.check_for_executable (tool)) {
            executable = tool;
            break;
          }
        }
      }

      debug ("ExtractFramesPostProcessor uses %s", executable);
      return executable;
    }
  }
}