OpenShot Video Editor  2.0.0
timeline.py
Go to the documentation of this file.
1 ##
2 #
3 # @file
4 # @brief This file contains a timeline object, which listens for updates and syncs a libopenshot timeline object
5 # @author Jonathan Thomas <jonathan@openshot.org>
6 #
7 # @section LICENSE
8 #
9 # Copyright (c) 2008-2016 OpenShot Studios, LLC
10 # (http://www.openshotstudios.com). This file is part of
11 # OpenShot Video Editor (http://www.openshot.org), an open-source project
12 # dedicated to delivering high quality video editing and animation solutions
13 # to the world.
14 #
15 # OpenShot Video Editor is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation, either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # OpenShot Video Editor is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
27 #
28 
29 import openshot # Python module for libopenshot (required video editing module installed separately)
30 
31 from classes.updates import UpdateInterface
32 from classes.logger import log
33 from classes.app import get_app
34 from classes import settings
35 
36 
37 ##
38 # This class syncs changes from the timeline to libopenshot
39 class TimelineSync(UpdateInterface):
40 
41  def __init__(self, window):
42  self.app = get_app()
43  self.window = window
44  project = self.app.project
46 
47  # Get some settings from the project
48  fps = project.get(["fps"])
49  width = project.get(["width"])
50  height = project.get(["height"])
51  sample_rate = project.get(["sample_rate"])
52  channels = project.get(["channels"])
53  channel_layout = project.get(["channel_layout"])
54 
55  # Create an instance of a libopenshot Timeline object
56  self.timeline = openshot.Timeline(width, height, openshot.Fraction(fps["num"], fps["den"]), sample_rate, channels,
57  channel_layout)
58  self.timeline.info.channel_layout = channel_layout
59  self.timeline.info.has_audio = True
60  self.timeline.info.has_video = True
61  self.timeline.info.video_length = 99999
62  self.timeline.info.duration = 999.99
63  self.timeline.info.sample_rate = sample_rate
64  self.timeline.info.channels = channels
65 
66  # Open the timeline reader
67  self.timeline.Open()
68 
69  # Add self as listener to project data updates (at the beginning of the list)
70  # This listener will receive events before others.
71  self.app.updates.add_listener(self, 0)
72 
73  ##
74  # This method is invoked by the UpdateManager each time a change happens (i.e UpdateInterface)
75  def changed(self, action):
76 
77  # Ignore changes that don't affect libopenshot
78  if len(action.key) >= 1 and action.key[0].lower() in ["files", "markers", "layers", "export_path"]:
79  return
80 
81  elif len(action.key) >= 1 and action.key[0].lower() in ["profile"]:
82  # Get speed of player
83  current_speed = self.window.preview_thread.player.Speed()
84 
85  # Stop preview thread
86  self.window.SpeedSignal.emit(0)
87 
88  # The timeline's profile changed, so update all clips
89  self.timeline.ApplyMapperToClips()
90 
91  # Resume speed
92  self.window.SpeedSignal.emit(current_speed)
93  return
94 
95  # Pass the change to the libopenshot timeline
96  try:
97  # Get speed of player
98  current_speed = self.window.preview_thread.player.Speed()
99 
100  # Stop preview thread
101  self.window.SpeedSignal.emit(0)
102 
103  if action.type == "load":
104  # This JSON is initially loaded to libopenshot to update the timeline
105  self.timeline.SetJson(action.json(only_value=True))
106  self.timeline.Open() # Re-Open the Timeline reader
107 
108  # Refresh current frame (since the entire timeline was updated)
109  self.window.refreshFrameSignal.emit()
110 
111  else:
112  # This JSON DIFF is passed to libopenshot to update the timeline
113  self.timeline.ApplyJsonDiff(action.json(is_array=True))
114 
115  # Resume speed
116  self.window.SpeedSignal.emit(current_speed)
117 
118  except Exception as e:
119  log.info("Error applying JSON to timeline object in libopenshot: %s" % e)
def get_app()
Returns the current QApplication instance of OpenShot.
Definition: app.py:54
This class syncs changes from the timeline to libopenshot.
Definition: timeline.py:39
def __init__(self, window)
Definition: timeline.py:41
def get_settings()
Get the current QApplication&#39;s settings instance.
Definition: settings.py:43
def changed(self, action)
This method is invoked by the UpdateManager each time a change happens (i.e UpdateInterface) ...
Definition: timeline.py:75