1
# OpenShot Video Editor is a program that creates, modifies, and edits video files.
2
# Copyright (C) 2009 Jonathan Thomas
4
# This file is part of OpenShot Video Editor (http://launchpad.net/openshot/).
6
# OpenShot Video Editor is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# OpenShot Video Editor is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with OpenShot Video Editor. If not, see <http://www.gnu.org/licenses/>.
19
from classes import messagebox
20
import os, sys, urllib, uuid
23
# init the foreign language
24
from language import Language_Init
26
########################################################################
28
"""The generic file object for OpenShot"""
30
#----------------------------------------------------------------------
31
def __init__(self, project=None):
32
self.project = project
37
# Add language support
38
translator = Language_Init.Translator(self.project)
39
_ = translator.lang.gettext
41
# init the variables for the File Object
42
self.name = "" # short / friendly name of the file
43
self.length = 0.0 # length in seconds
44
self.videorate = (30,0) # audio rate or video framerate
45
self.file_type = "" # video, audio, image, image sequence
50
self.label = "" # user description of the file
51
self.thumb_location = "" # file uri of preview thumbnail
52
self.ttl = 1 # time-to-live - only used for image sequence. Represents the # of frames per image.
54
self.unique_id = str(uuid.uuid1())
56
self.project = project # reference to project
59
def get_thumbnail(self, width, height):
60
"""Get and resize the pixbuf thumbnail for a file"""
62
# get the thumbnail (or load default)
64
if self.thumb_location:
65
pbThumb = gtk.gdk.pixbuf_new_from_file(self.thumb_location)
67
# Load the No Thumbnail Picture
68
pbThumb = gtk.gdk.pixbuf_new_from_file(os.path.join(self.project.IMAGE_DIR, "AudioThumbnail.png"))
71
# Load the No Thumbnail Picture
72
pbThumb = gtk.gdk.pixbuf_new_from_file(os.path.join(self.project.IMAGE_DIR, "AudioThumbnail.png"))
75
return pbThumb.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
78
########################################################################
80
"""The generic folder object for OpenShot"""
82
#----------------------------------------------------------------------
83
def __init__(self, project=None):
86
# Init the variables for the Folder Object
87
self.name = "" # short / friendly name of the folder
88
self.location = "" # file system location
90
self.project = project
92
# init the list of files & folders
93
# this list can contain OpenShotFolder or OpenShotFile objects
94
# the order of this list determines the order of the tree items
97
# this queue holds files that are currently being added. this prevents
98
# duplicate files to be added at the same time
102
#----------------------------------------------------------------------
103
def AddFolder(self, folder_name, project=None):
105
"""Add a new folder to the current folder"""
106
#does this folder name already exist?
107
if self.FindFolder(folder_name):
108
messagebox.show(_("OpenShot Error"), _("The folder %s already exists in this project." % folder_name))
110
newFolder = OpenShotFolder(project)
111
newFolder.name = folder_name
113
self.items.append(newFolder)
114
self.project.form.refresh()
117
#----------------------------------------------------------------------
118
def AddFile(self, file_name):
119
"""Add a new file to the current folder"""
123
# clean path to video
124
file_name = urllib.unquote(file_name)
126
# don't add a file that is alrady in this folder (i.e. dupe check)
127
if self.file_exists_in_project(file_name):
130
# check if the path is a 'folder' and not a file
131
if os.path.isdir(file_name):
133
# loop through each sub-file (if any)
134
for sub_file in os.listdir(file_name):
135
sub_file_path = os.path.join(file_name, sub_file)
138
if os.path.isfile(sub_file_path):
140
# don't add a file that is alrady in this folder (i.e. dupe check)
141
if self.file_exists_in_project(sub_file_path) == False:
143
# inspect the media file and generate it's thumbnail image (if any)
144
newFile = self.project.thumbnailer.GetFile(sub_file_path)
146
# add to internal item collection
148
self.items.append(newFile)
151
# inspect the media file and generate it's thumbnail image (if any)
152
newFile = self.project.thumbnailer.GetFile(file_name)
154
# add to internal item collection
156
self.items.append(newFile)
160
#----------------------------------------------------------------------
161
def ConvertFileToImages(self, file_name):
162
"""Add a new file to the current folder"""
166
# clean path to video
167
file_name = urllib.unquote(file_name)
169
# check if this file is already in the project
170
for item in self.items:
171
if file_name in item.name and item.file_type == _("Image Sequence").lower():
174
# inspect the media file and generate it's thumbnail image (if any)
175
newFile = self.project.thumbnailer.GetFile(file_name, all_frames=True)
177
# update the location
179
(dirName, fname) = os.path.split(file_name)
180
(fileBaseName, fileExtension)=os.path.splitext(fname)
181
newFile.name = os.path.join(dirName, fileBaseName, fileBaseName + "_%d.png")
183
# add to internal item collection
184
self.items.append(newFile)
189
def file_exists_in_project(self, file_name):
190
""" Check if this file exists in this project """
192
# don't add a file that is alrady in this folder (i.e. dupe check)
193
for item in self.items:
194
if file_name in item.name:
197
# didn't find a match
200
#----------------------------------------------------------------------
201
def get_file_path_from_dnd_dropped_uri(self, uri):
205
#path = urllib.url2pathname(uri) # escape special chars
206
path = path.strip('\r\n\x00') # remove \r\n and NULL
209
path = path.replace("/", "\\")
210
path = path.replace("%20", " ")
212
# get the path to file
213
if path.startswith('file:\\\\\\'): # windows
214
path = path[8:] # 8 is len('file:///')
215
elif path.startswith('file://'): # nautilus, rox
216
path = path[7:] # 7 is len('file://')
217
elif path.startswith('file:'): # xffm
218
path = path[5:] # 5 is len('file:')
222
def UpdateFileLabel(self, filename, value, refresh_tree=0):
223
#this will only be called when the treeview mode is selected, not the thumbview
224
for item in self.items:
225
if item.name.endswith(filename):
228
if refresh_tree == 1:
229
# Update the main form
230
self.project.form.refresh()
233
def RemoveFile(self, filename):
234
item = self.FindFile(filename)
236
# mark project as modified
237
self.project.is_modified = True
239
# find clips that have this file object
240
for track in self.project.sequences[0].tracks:
241
for clip in reversed(track.clips):
242
# does clip match file
243
if clip.file_object == item:
245
track.clips.remove(clip)
246
# remove from file collection
247
self.items.remove(item)
250
item = self.FindFolder(filename)
252
# remove from file collection
253
self.items.remove(item)
256
#----------------------------------------------------------------------
257
def FindFile(self, file_name):
258
"""Pass the file system location of the item you
259
are looking for and this function will return the
260
reference to the OpenShot File that matches"""
262
# loop through all files looking for the matching filename
263
for file in self.items:
264
if isinstance(file, OpenShotFile):
267
# check if file name matches this file
268
if file_name == name:
271
# split file name (if it's not found)
272
(dirName, fileName) = os.path.split(name)
274
# check if file name matches the basefile name
275
if file_name == fileName:
283
#----------------------------------------------------------------------
284
def FindFileByID(self, unique_id):
285
"""Pass the file system location of the item you
286
are looking for and this function will return the
287
reference to the OpenShot File that matches"""
289
# loop through all files looking for the matching filename
290
for file in self.items:
291
if isinstance(file, OpenShotFile):
293
# check if file name matches this file
294
if unique_id == file.unique_id:
301
def FindFolder(self, folder_name):
302
"""Returns a reference to the OpenShotFolder
303
that matches the folder_name"""
304
for folder in self.items:
305
if isinstance(folder, OpenShotFolder):
308
if folder_name == name:
311
def ListFolders(self):
312
"""Return a list of any folders in the project"""
315
for item in self.items:
316
if isinstance(item, OpenShotFolder):
317
folders.append(item.name)
320
def AddParentToFile(self, file_name, folder_name):
321
file = self.FindFile(file_name)
323
file.parent = folder_name
325
def RemoveParent(self, file_name, folder_name):
326
file = self.FindFile(file_name)
328
print "REMOVE PARENT"
329
file.parent = self.project.project_folder