[shotwell/wip/phako/128: 4/4] video-support: Add external reader process




commit 20eac2a5009960a90f9bae18ca1983400a0a5403
Author: Jens Georg <mail jensge org>
Date:   Wed Dec 23 15:13:07 2020 +0100

    video-support: Add external reader process

 src/AppDirs.vala                                  | 10 ++++
 src/video-support/VideoMetadataReaderProcess.vala | 56 +++++++++++++++++++++++
 src/video-support/VideoReader.vala                | 41 ++++++++++-------
 src/video-support/meson.build                     | 14 ++++++
 4 files changed, 105 insertions(+), 16 deletions(-)
---
diff --git a/src/AppDirs.vala b/src/AppDirs.vala
index ba1a14ce..f82fe4f4 100644
--- a/src/AppDirs.vala
+++ b/src/AppDirs.vala
@@ -321,6 +321,16 @@ class AppDirs {
         return f;
     }
 
+    public static File get_metadata_helper() {
+        const string filename = "shotwell-video-metadata-handler";
+        File f = AppDirs.get_libexec_dir().get_child("video-support").get_child (filename);
+        if (!f.query_exists()) {
+            // If we're running installed.
+            f = AppDirs.get_libexec_dir () .get_child ("shotwell").get_child (filename);
+        }
+        return f;
+    }
+
     public static File get_settings_migrator_bin() {
         const string filename = "shotwell-settings-migrator";
         File f = AppDirs.get_libexec_dir().get_child("settings-migrator").get_child (filename);
diff --git a/src/video-support/VideoMetadataReaderProcess.vala 
b/src/video-support/VideoMetadataReaderProcess.vala
new file mode 100644
index 00000000..cba61837
--- /dev/null
+++ b/src/video-support/VideoMetadataReaderProcess.vala
@@ -0,0 +1,56 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+using Gst;
+using Gst.PbUtils;
+
+int main(string[] args) {
+    Intl.setlocale(GLib.LocaleCategory.NUMERIC, "C");
+
+    var option_context = new OptionContext("- shotwell video metadata reader helper binary");
+    option_context.set_help_enabled(true);
+    option_context.add_group(Gst.init_get_option_group());
+
+    double clip_duration;
+    GLib.DateTime timestamp = null;
+
+    try {
+        option_context.parse(ref args);
+
+        if (args.length < 2)
+            throw new IOError.INVALID_ARGUMENT("Missing URI");
+
+        var f = File.new_for_commandline_arg (args[1]);
+
+        Gst.PbUtils.Discoverer d = new Gst.PbUtils.Discoverer((Gst.ClockTime) (Gst.SECOND * 5));
+        Gst.PbUtils.DiscovererInfo info = d.discover_uri(f.get_uri());
+
+        clip_duration = ((double) info.get_duration()) / 1000000000.0;
+
+        // Get creation time.
+        // TODO: Note that TAG_DATE can be changed to TAG_DATE_TIME in the future
+        // (and the corresponding output struct) in order to implement #2836.
+        Date? video_date = null;
+        if (info.get_tags() != null && info.get_tags().get_date(Gst.Tags.DATE, out video_date)) {
+            // possible for get_date() to return true and a null Date
+            if (video_date != null) {
+                timestamp = new GLib.DateTime.local(video_date.get_year(), video_date.get_month(),
+                    video_date.get_day(), 0, 0, 0);
+            }
+        }
+
+        print("%.3f\n", clip_duration);
+        if (timestamp != null) {
+            print("%s\n", timestamp.format_iso8601());
+        } else {
+            print("none\n");
+        }
+    } catch (Error error) {
+        critical("Failed to parse options: %s", error.message);
+
+        return 1;
+    }
+
+    return 0;
+}
diff --git a/src/video-support/VideoReader.vala b/src/video-support/VideoReader.vala
index 8343facd..b7e855d7 100644
--- a/src/video-support/VideoReader.vala
+++ b/src/video-support/VideoReader.vala
@@ -180,28 +180,37 @@ public class VideoReader {
             throw new VideoError.FILE("video file '%s' does not exist or is inaccessible".printf(
                 file.get_path()));
 
+        uint id = 0;
         try {
-            Gst.PbUtils.Discoverer d = new Gst.PbUtils.Discoverer((Gst.ClockTime) (Gst.SECOND * 5));
-            Gst.PbUtils.DiscovererInfo info = d.discover_uri(file.get_uri());
-
-            clip_duration = ((double) info.get_duration()) / 1000000000.0;
-
-            // Get creation time.
-            // TODO: Note that TAG_DATE can be changed to TAG_DATE_TIME in the future
-            // (and the corresponding output struct) in order to implement #2836.
-            Date? video_date = null;
-            if (info.get_tags() != null && info.get_tags().get_date(Gst.Tags.DATE, out video_date)) {
-                // possible for get_date() to return true and a null Date
-                if (video_date != null) {
-                    timestamp = new DateTime.local(video_date.get_year(), video_date.get_month(),
-                        video_date.get_day(), 0, 0, 0);
-                }
-            }
+            var cancellable = new Cancellable();
+
+            id = Timeout.add_seconds(10, () => {
+                cancellable.cancel();
+                id = 0;
+
+                return false;
+            });
+
+            Bytes stdout_buf = null;
+
+            var process = new GLib.Subprocess(GLib.SubprocessFlags.STDOUT_PIPE, 
AppDirs.get_metadata_helper().get_path(), file.get_uri());
+            process.communicate(null, cancellable, out stdout_buf, null);
+            string[] lines = ((string) stdout_buf.get_data()).split("\n");
+
+            var old = Intl.setlocale(GLib.LocaleCategory.NUMERIC, "C");
+            clip_duration = double.parse(lines[0]);
+            Intl.setlocale(GLib.LocaleCategory.NUMERIC, old);
+            if (lines[1] != "none")
+                timestamp = new DateTime.from_iso8601(lines[1], null);
         } catch (Error e) {
             debug("Video read error: %s", e.message);
             throw new VideoError.CONTENTS("GStreamer couldn't extract clip information: %s"
                 .printf(e.message));
         }
+
+        if (id != 0) {
+            Source.remove(id);
+        }
     }
 
     // Used by thumbnailer() to kill the external process if need be.
diff --git a/src/video-support/meson.build b/src/video-support/meson.build
index ac7e5646..da3f9d73 100644
--- a/src/video-support/meson.build
+++ b/src/video-support/meson.build
@@ -1,3 +1,17 @@
+executable(
+    'shotwell-video-metadata-handler',
+    [
+        'VideoMetadataReaderProcess.vala'
+    ],
+    dependencies : [
+        gio,
+        gstreamer,
+        gstreamer_pbu
+    ],
+    c_args : '-DGST_PB_UTILS_IS_DISCOVERER_INFO=GST_IS_DISCOVERER_INFO'
+    # Work-around for wrong type-check macro generated by valac
+)
+
 libvideometadata_handling = static_library(
     'video_metadata_handling',
     [


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]