From f237d0aff3dfbd2d118dcadeaa34a2b1e7137664 Mon Sep 17 00:00:00 2001
From: Antonio Ospite <ao2@ao2.it>
Date: Wed, 7 Dec 2016 16:26:45 +0100
Subject: [PATCH 1/1] Add a title clip to the timeline

---
 vidi-timeline.py | 21 ++++++++++++++++++---
 vidi/Timeline.py | 21 +++++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/vidi-timeline.py b/vidi-timeline.py
index 17165fc..07639e6 100755
--- a/vidi-timeline.py
+++ b/vidi-timeline.py
@@ -22,15 +22,30 @@ import sys
 import mido
 import vidi
 
-# TODO: turn that into a command line option
+# TODO: turn these into command line options
 ADD_REST_BACKGROUND = True
+ADD_TITLE = True
+TITLE_TEXT = None
+TITLE_DURATION = 5
+TITLE_CREDITS = "Created with vidi-timeline\nhttps://git.ao2.it/vidi-player.git"
 
 
 def timeline_from_midi(midi_file, video_font_path):
     timeline = vidi.Timeline()
 
-    elapsed_time = 0
-    start_time = 0
+    if ADD_TITLE:
+        title = TITLE_TEXT
+        if not title:
+            title = os.path.splitext(os.path.basename(midi_file.filename))[0]
+            title += "\n\n"
+            title += TITLE_CREDITS
+
+        timeline.add_title_clip(title, 0, TITLE_DURATION)
+
+        elapsed_time = start_time = TITLE_DURATION
+    else:
+        elapsed_time = start_time = 0
+
     for msg in midi_file:
         elapsed_time += msg.time
         if vidi.is_note_on(msg) and msg.channel == 0:
diff --git a/vidi/Timeline.py b/vidi/Timeline.py
index 59c891a..da8e83c 100755
--- a/vidi/Timeline.py
+++ b/vidi/Timeline.py
@@ -29,6 +29,10 @@ GES.init()
 
 from .Player import Player
 
+TITLE_BACKGROUND = 0xFF000000
+TITLE_COLOR = 0xFFFFFFFF
+TITLE_OUTLINE_COLOR = 0x00000000
+TITLE_FONT_DESC = "Georgia, 24"
 
 class Timeline(object):
     def __init__(self):
@@ -47,6 +51,23 @@ class Timeline(object):
         ges_pipeline.set_timeline(self.timeline)
         self.player = Player(ges_pipeline)
 
+    def add_title_clip(self, text, start_time, duration):
+        title_clip = GES.TitleClip()
+        title_clip.set_start(start_time * Gst.SECOND)
+        title_clip.set_duration(duration * Gst.SECOND)
+        self.layer.add_clip(title_clip)
+
+        # Now that the clip is inserted in the timeline, it has a source which
+        # can be used to set its properties. (comment taken from Pitivi)
+        title_source = title_clip.find_track_element(None, GES.TitleSource)
+        title_source.set_child_property("text", text)
+        title_source.set_child_property("background", TITLE_BACKGROUND)
+        title_source.set_child_property("color", TITLE_COLOR)
+        title_source.set_child_property("outline-color", TITLE_OUTLINE_COLOR)
+        title_source.set_child_property("font-desc", TITLE_FONT_DESC)
+        title_source.set_child_property("halignment", GES.TextVAlign.ABSOLUTE)
+        title_source.set_child_property("valignment", GES.TextHAlign.ABSOLUTE)
+
     def add_clip(self, clip_path, start_time, duration):
         clip_uri = Gst.filename_to_uri(clip_path)
         asset = GES.UriClipAsset.request_sync(clip_uri)
-- 
2.1.4