da8e83ce2f711d47b2e5c932e44a361062107e14
[vidi-player.git] / vidi / Timeline.py
1 #!/usr/bin/env python3
2 #
3 # Timeline - very simple GES timeline wrapper
4 #
5 # Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import gi
21
22 gi.require_version('Gst', '1.0')
23 from gi.repository import Gst
24 Gst.init(None)
25
26 gi.require_version('GES', '1.0')
27 from gi.repository import GES
28 GES.init()
29
30 from .Player import Player
31
32 TITLE_BACKGROUND = 0xFF000000
33 TITLE_COLOR = 0xFFFFFFFF
34 TITLE_OUTLINE_COLOR = 0x00000000
35 TITLE_FONT_DESC = "Georgia, 24"
36
37 class Timeline(object):
38     def __init__(self):
39         self.project = GES.Project(extractable_type=GES.Timeline)
40         self.timeline = GES.Asset.extract(self.project)
41
42         audio_track = GES.AudioTrack.new()
43         video_track = GES.VideoTrack.new()
44
45         self.timeline.add_track(audio_track)
46         self.timeline.add_track(video_track)
47
48         self.layer = self.timeline.append_layer()
49
50         ges_pipeline = GES.Pipeline()
51         ges_pipeline.set_timeline(self.timeline)
52         self.player = Player(ges_pipeline)
53
54     def add_title_clip(self, text, start_time, duration):
55         title_clip = GES.TitleClip()
56         title_clip.set_start(start_time * Gst.SECOND)
57         title_clip.set_duration(duration * Gst.SECOND)
58         self.layer.add_clip(title_clip)
59
60         # Now that the clip is inserted in the timeline, it has a source which
61         # can be used to set its properties. (comment taken from Pitivi)
62         title_source = title_clip.find_track_element(None, GES.TitleSource)
63         title_source.set_child_property("text", text)
64         title_source.set_child_property("background", TITLE_BACKGROUND)
65         title_source.set_child_property("color", TITLE_COLOR)
66         title_source.set_child_property("outline-color", TITLE_OUTLINE_COLOR)
67         title_source.set_child_property("font-desc", TITLE_FONT_DESC)
68         title_source.set_child_property("halignment", GES.TextVAlign.ABSOLUTE)
69         title_source.set_child_property("valignment", GES.TextHAlign.ABSOLUTE)
70
71     def add_clip(self, clip_path, start_time, duration):
72         clip_uri = Gst.filename_to_uri(clip_path)
73         asset = GES.UriClipAsset.request_sync(clip_uri)
74         self.layer.add_asset(asset, start_time * Gst.SECOND, 0,
75                              duration * Gst.SECOND, GES.TrackType.UNKNOWN)
76
77     def add_layer_clip(self, clip_path, start_time, duration):
78         """Add a clip on its own layer"""
79         clip_uri = Gst.filename_to_uri(clip_path)
80         asset = GES.UriClipAsset.request_sync(clip_uri)
81         new_layer = self.timeline.append_layer()
82         new_layer.add_asset(asset, start_time * Gst.SECOND, 0,
83                             duration * Gst.SECOND, GES.TrackType.UNKNOWN)
84
85     def play(self):
86         self.timeline.commit()
87         self.player.play()
88
89     def stop(self):
90         self.player.stop()
91
92     def save(self, path):
93         uri = Gst.filename_to_uri(path)
94         self.project.save(self.timeline, uri, None, False)