def add_file(self, filepath): """ Add an animation to the project file tree """ path, filename = os.path.split(filepath) # Add file into project app = get_app() _ = get_app()._tr # Check for this path in our existing project data file = File.get(path=filepath) # If this file is already found, exit if file: return # Get the JSON for the clip's internal reader try: # Open image sequence in FFmpegReader reader = openshot.FFmpegReader(filepath) reader.Open() # Serialize JSON for the reader file_data = json.loads(reader.Json()) # Set media type file_data["media_type"] = "video" # Save new file to the project data file = File() file.data = file_data file.save() return True except: # Handle exception msg = QMessageBox() msg.setText( _("{} is not a valid video, audio, or image file.".format( filename))) msg.exec_() return False
def LoadFile(self, path=None): """ Load a media file into the video player """ s = settings.get_settings() # Check to see if this path is already loaded # TODO: Determine why path is passed in as an empty string instead of None if path == self.clip_path or (not path and not self.clip_path): return # Determine the current frame of the timeline (when switching to a clip) seek_position = 1 if path and not self.clip_path: # Track the current frame self.original_position = self.player.Position() # Stop player (very important to prevent crashing) self.original_speed = self.player.Speed() self.player.Speed(0) # If blank path, switch back to self.timeline reader if not path: # Return to self.timeline reader log.info("Set timeline reader again in player: %s" % self.timeline) self.player.Reader(self.timeline) # Clear clip reader reference self.clip_reader = None self.clip_path = None # Switch back to last timeline position seek_position = self.original_position else: # Get extension of media path ext = os.path.splitext(path) # Load Reader based on extension new_reader = None if ext in [ '.avi', 'mov', 'mkv', 'mpg', 'mpeg', 'mp3', 'mp4', 'mts', 'ogg', 'wav', 'wmv', 'webm', 'vob' ]: try: new_reader = openshot.FFmpegReader(path) new_reader.Open() except: try: new_reader = openshot.QtImageReader(path) new_reader.Open() except: log.error( 'Failed to load media file into video player: %s' % path) return else: try: new_reader = openshot.QtImageReader(path) new_reader.Open() except: try: new_reader = openshot.FFmpegReader(path) new_reader.Open() except: log.error( 'Failed to load media file into video player: %s' % path) return # Wrap reader in FrameMapper (to match current settings of timeline) new_mapper = openshot.FrameMapper( new_reader, self.timeline.info.fps, openshot.PULLDOWN_NONE, self.timeline.info.sample_rate, self.timeline.info.channels, self.timeline.info.channel_layout) # Keep track of previous clip readers (so we can Close it later) self.previous_clip_mappers.append(new_mapper) self.previous_clip_readers.append(new_reader) # Assign new clip_reader self.clip_reader = new_mapper self.clip_path = path # Open reader self.clip_reader.Open() log.info("Set new FrameMapper reader in player: %s" % self.clip_reader) self.player.Reader(self.clip_reader) # Close and destroy old clip readers (leaving the 3 most recent) while len(self.previous_clip_readers) > 3: log.info('Removing old clip reader: %s' % self.previous_clip_readers[0]) self.previous_clip_mappers.pop(0) self.previous_clip_readers.pop(0) # Seek to frame 1, and resume speed self.player.Seek(seek_position) self.player.Speed(self.original_speed)
def __init__(self, file=None): # Create dialog class QDialog.__init__(self) # Load UI from designer ui_util.load_ui(self, self.ui_path) # Init UI ui_util.init_ui(self) # Track metrics track_metric_screen("cutting-screen") self.start_frame = 1 self.start_image = None self.end_frame = 1 self.end_image = None # Keep track of file object self.file = file self.file_path = file.absolute_path() self.video_length = int(file.data['video_length']) self.fps_num = int(file.data['fps']['num']) self.fps_den = int(file.data['fps']['den']) self.fps = float(self.fps_num) / float(self.fps_den) # Open video file with Reader log.info(self.file_path) self.r = openshot.FFmpegReader(self.file_path) self.r.Open() # Add Video Widget self.videoPreview = VideoWidget() self.videoPreview.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding) self.verticalLayout.insertWidget(0, self.videoPreview) # Start the preview thread self.preview_parent = PreviewParent() self.preview_parent.Init(self, self.r, self.videoPreview) self.preview_thread = self.preview_parent.worker # Set slider constraints self.sliderIgnoreSignal = False self.sliderVideo.setMinimum(1) self.sliderVideo.setMaximum(self.video_length) self.sliderVideo.setSingleStep(1) self.sliderVideo.setSingleStep(1) self.sliderVideo.setPageStep(24) # Determine if a start or end attribute is in this file start_frame = 1 if 'start' in self.file.data.keys(): start_frame = (float(self.file.data['start']) * self.fps) + 1 # Display start frame (and then the previous frame) QTimer.singleShot( 500, functools.partial(self.sliderVideo.setValue, start_frame + 1)) QTimer.singleShot( 600, functools.partial(self.sliderVideo.setValue, start_frame)) # Connect signals self.btnPlay.clicked.connect(self.btnPlay_clicked) self.sliderVideo.valueChanged.connect(self.sliderVideo_valueChanged) self.btnStart.clicked.connect(self.btnStart_clicked) self.btnEnd.clicked.connect(self.btnEnd_clicked) self.btnClear.clicked.connect(self.btnClear_clicked) self.btnAddClip.clicked.connect(self.btnAddClip_clicked)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>. # This can be run against an uninstalled build of libopenshot, just set the # environment variable PYTHONPATH to the location of the Python bindings. # # For example: # $ PYTHONPATH=../../build/src/bindings/python python3 Example.py # import openshot # Create an FFmpegReader r = openshot.FFmpegReader("sintel_trailer-720p.mp4") r.Open() # Open the reader r.DisplayInfo() # Display metadata # Set up Writer w = openshot.FFmpegWriter("pythonExample.mp4") w.SetAudioOptions(True, "libmp3lame", r.info.sample_rate, r.info.channels, r.info.channel_layout, 128000) w.SetVideoOptions(True, "libx264", openshot.Fraction(30000, 1000), 1280, 720, openshot.Fraction(1, 1), False, False, 3000000) w.info.metadata["title"] = "testtest" w.info.metadata["artist"] = "aaa" w.info.metadata["album"] = "bbb"