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/>.
26
# init the foriegn language
27
import language.Language_Init as Language_Init
29
########################################################################
31
"""The generic file object for OpenShot"""
33
#----------------------------------------------------------------------
34
def __init__(self, project=None):
37
# Add language support
38
translator = Language_Init.Translator()
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 path of OpenShot
63
path = os.path.dirname(os.path.abspath(sys.argv[0]))
65
# get the thumbnail (or load default)
67
if self.thumb_location:
68
pbThumb = gtk.gdk.pixbuf_new_from_file(self.thumb_location)
70
# Load the No Thumbnail Picture
71
pbThumb = gtk.gdk.pixbuf_new_from_file("%s/images/AudioThumbnail.png" % (path))
74
# Load the No Thumbnail Picture
75
pbThumb = gtk.gdk.pixbuf_new_from_file("%s/images/AudioThumbnail.png" % (path))
78
return pbThumb.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)
81
########################################################################
83
"""The generic folder object for OpenShot"""
85
#----------------------------------------------------------------------
86
def __init__(self, project=None):
89
# Init the variables for the Folder Object
90
self.name = "" # short / friendly name of the folder
91
self.location = "" # file system location
93
self.project = project
95
# init the list of files & folders
96
# this list can contain OpenShotFolder or OpenShotFile objects
97
# the order of this list determines the order of the tree items
100
# this queue holds files that are currently being added. this prevents
101
# duplicate files to be added at the same time
105
#----------------------------------------------------------------------
106
def AddFolder(self, folder_name, project=None):
108
"""Add a new folder to the current folder"""
109
#does this folder name already exist?
110
if self.FindFolder(folder_name):
111
messagebox.show(_("OpenShot Error"), _("The folder %s already exists in this project." % folder_name))
113
newFolder = OpenShotFolder(project)
114
newFolder.name = folder_name
116
self.items.append(newFolder)
117
self.project.form.refresh()
120
#----------------------------------------------------------------------
121
def AddFile(self, file_name):
122
"""Add a new file to the current folder"""
126
# clean path to video
127
file_name = urllib.unquote(file_name)
129
# don't add a file that is alrady in this folder (i.e. dupe check)
130
if self.file_exists_in_project(file_name):
133
# check if the path is a 'folder' and not a file
134
if os.path.isdir(file_name):
136
# loop through each sub-file (if any)
137
for sub_file in os.listdir(file_name):
138
sub_file_path = os.path.join(file_name, sub_file)
141
if os.path.isfile(sub_file_path):
143
# don't add a file that is alrady in this folder (i.e. dupe check)
144
if self.file_exists_in_project(sub_file_path) == False:
146
# inspect the media file and generate it's thumbnail image (if any)
147
newFile = self.project.thumbnailer.GetFile(sub_file_path)
149
# add to internal item collection
151
self.items.append(newFile)
154
# inspect the media file and generate it's thumbnail image (if any)
155
newFile = self.project.thumbnailer.GetFile(file_name)
157
# add to internal item collection
159
self.items.append(newFile)
163
#----------------------------------------------------------------------
164
def ConvertFileToImages(self, file_name):
165
"""Add a new file to the current folder"""
169
# clean path to video
170
file_name = urllib.unquote(file_name)
172
# check if this file is already in the project
173
for item in self.items:
174
if file_name in item.name and item.file_type == _("Image Sequence").lower():
177
# inspect the media file and generate it's thumbnail image (if any)
178
newFile = self.project.thumbnailer.GetFile(file_name, all_frames=True)
180
# update the location
182
(dirName, fname) = os.path.split(file_name)
183
(fileBaseName, fileExtension)=os.path.splitext(fname)
184
newFile.name = os.path.join(dirName, fileBaseName, fileBaseName + "_%d.png")
186
# add to internal item collection
187
self.items.append(newFile)
192
def file_exists_in_project(self, file_name):
193
""" Check if this file exists in this project """
195
# don't add a file that is alrady in this folder (i.e. dupe check)
196
for item in self.items:
197
if file_name in item.name:
200
# didn't find a match
203
#----------------------------------------------------------------------
204
def get_file_path_from_dnd_dropped_uri(self, uri):
208
#path = urllib.url2pathname(uri) # escape special chars
209
path = path.strip('\r\n\x00') # remove \r\n and NULL
212
path = path.replace("/", "\\")
213
path = path.replace("%20", " ")
215
# get the path to file
216
if path.startswith('file:\\\\\\'): # windows
217
path = path[8:] # 8 is len('file:///')
218
elif path.startswith('file://'): # nautilus, rox
219
path = path[7:] # 7 is len('file://')
220
elif path.startswith('file:'): # xffm
221
path = path[5:] # 5 is len('file:')
225
def UpdateFileLabel(self, filename, value, refresh_tree=0):
226
#this will only be called when the treeview mode is selected, not the thumbview
227
for item in self.items:
228
if item.name.endswith(filename):
231
if refresh_tree == 1:
232
# Update the main form
233
self.project.form.refresh()
236
def RemoveFile(self, filename):
237
item = self.FindFile(filename)
239
# mark project as modified
240
self.project.is_modified = True
242
# find clips that have this file object
243
for track in self.project.sequences[0].tracks:
244
for clip in reversed(track.clips):
245
# does clip match file
246
if clip.file_object == item:
248
track.clips.remove(clip)
249
# remove from file collection
250
self.items.remove(item)
253
item = self.FindFolder(filename)
255
# remove from file collection
256
self.items.remove(item)
259
#----------------------------------------------------------------------
260
def FindFile(self, file_name):
261
"""Pass the file system location of the item you
262
are looking for and this function will return the
263
reference to the OpenShot File that matches"""
265
# loop through all files looking for the matching filename
266
for file in self.items:
267
if isinstance(file, OpenShotFile):
270
# check if file name matches this file
271
if file_name == name:
274
# split file name (if it's not found)
275
(dirName, fileName) = os.path.split(name)
277
# check if file name matches the basefile name
278
if file_name == fileName:
286
#----------------------------------------------------------------------
287
def FindFileByID(self, unique_id):
288
"""Pass the file system location of the item you
289
are looking for and this function will return the
290
reference to the OpenShot File that matches"""
292
# loop through all files looking for the matching filename
293
for file in self.items:
294
if isinstance(file, OpenShotFile):
296
# check if file name matches this file
297
if unique_id == file.unique_id:
304
def FindFolder(self, folder_name):
305
"""Returns a reference to the OpenShotFolder
306
that matches the folder_name"""
307
for folder in self.items:
308
if isinstance(folder, OpenShotFolder):
311
if folder_name == name:
314
def ListFolders(self):
315
"""Return a list of any folders in the project"""
318
for item in self.items:
319
if isinstance(item, OpenShotFolder):
320
folders.append(item.name)
323
def AddParentToFile(self, file_name, folder_name):
324
file = self.FindFile(file_name)
326
file.parent = folder_name
328
def RemoveParent(self, file_name, folder_name):
329
file = self.FindFile(file_name)
331
print "REMOVE PARENT"
332
file.parent = self.project.project_folder