~openshot.code/openshot/main

« back to all changes in this revision

Viewing changes to openshot/classes/files.py

  • Committer: Jonathan Thomas
  • Date: 2009-09-08 04:49:08 UTC
  • Revision ID: jonathan@jonathan64-20090908044908-kzhw2m1dl251yt9y
Bumping version to 0.9.30

Here it goes.  Massive re-factoring across OpenShot.  I put
a ton of regression work into this to ensure everything still
works, but as always, I could have missed something.

The biggest changes: 
------------------------------
1) Distutils is now used to install OpenShot (setup.py install).
   Installing OpenShot this way will copy Mime Types, Register Icons,
   Add launcher to Application menu, and copy the OpenShot .py code 
   to the /site-packages/ folder.
2) Python code moved into ~/openshot/openshot/
3) New folders ~/openshot/[bin,docs,xdg]
4) Translations moved to ~/openshot/openshot/locale
5) classes/project.py contains all of the PATH variables
6) classes/info.py contains the version of OpenShot
7) after installing (using setup.py), the /openshot/bin/openshot 
   is the launcher that gets copied to the /usr/bin
8) A few bug fixes have also been added:
   A) removing marker clears timeline
   B) opening a project stopped some changes from refreshing the video
9) Arguments can be passed to OpenShot ($ openshot 'video1.avi', 'video2.avi')
------------------------------

There are now 2 ways to launch OpenShot.

$ openshot (assuming setup.py was used to install OpenShot)
$ ~/openshot/openshot/openshot.py  (I know... it looks funny)

Good luck to everyone testing this!  =)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#       OpenShot Video Editor is a program that creates, modifies, and edits video files.
 
2
#   Copyright (C) 2009  Jonathan Thomas
 
3
#
 
4
#       This file is part of OpenShot Video Editor (http://launchpad.net/openshot/).
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
 
 
19
from classes import messagebox
 
20
import os, sys, urllib, uuid
 
21
import gtk
 
22
 
 
23
# init the foreign language
 
24
from language import Language_Init
 
25
 
 
26
########################################################################
 
27
class OpenShotFile:
 
28
        """The generic file object for OpenShot"""
 
29
 
 
30
        #----------------------------------------------------------------------
 
31
        def __init__(self, project=None):
 
32
                self.project = project
 
33
                
 
34
                """Constructor"""
 
35
                
 
36
                
 
37
                # Add language support
 
38
                translator = Language_Init.Translator(self.project)
 
39
                _ = translator.lang.gettext
 
40
 
 
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
 
46
                self.max_frames = 0.0
 
47
                self.fps = 0.0
 
48
                self.height = 0
 
49
                self.width = 0
 
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.
 
53
                
 
54
                self.unique_id = str(uuid.uuid1())      
 
55
                self.parent = None
 
56
                self.project = project    # reference to project
 
57
                
 
58
                
 
59
        def get_thumbnail(self, width, height):
 
60
                """Get and resize the pixbuf thumbnail for a file"""    
 
61
                
 
62
                # get the thumbnail (or load default)
 
63
                try:
 
64
                        if self.thumb_location:
 
65
                                pbThumb = gtk.gdk.pixbuf_new_from_file(self.thumb_location)
 
66
                        else:
 
67
                                # Load the No Thumbnail Picture
 
68
                                pbThumb = gtk.gdk.pixbuf_new_from_file(os.path.join(self.project.IMAGE_DIR, "AudioThumbnail.png"))
 
69
                except:
 
70
 
 
71
                                # Load the No Thumbnail Picture
 
72
                                pbThumb = gtk.gdk.pixbuf_new_from_file(os.path.join(self.project.IMAGE_DIR, "AudioThumbnail.png"))
 
73
 
 
74
                # resize thumbnail
 
75
                return pbThumb.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)     
 
76
                
 
77
 
 
78
########################################################################
 
79
class OpenShotFolder:
 
80
        """The generic folder object for OpenShot"""
 
81
 
 
82
        #----------------------------------------------------------------------
 
83
        def __init__(self, project=None):
 
84
                """Constructor"""
 
85
                
 
86
                # Init the variables for the Folder Object
 
87
                self.name = ""            # short / friendly name of the folder
 
88
                self.location = ""        # file system location
 
89
                self.parent = None
 
90
                self.project = project
 
91
                
 
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
 
95
                self.items = []
 
96
                
 
97
                # this queue holds files that are currently being added. this prevents
 
98
                # duplicate files to be added at the same time
 
99
                self.queue = []
 
100
 
 
101
 
 
102
        #----------------------------------------------------------------------
 
103
        def AddFolder(self, folder_name, project=None):
 
104
                
 
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))
 
109
                        return
 
110
                newFolder = OpenShotFolder(project)             
 
111
                newFolder.name = folder_name
 
112
                
 
113
                self.items.append(newFolder)
 
114
                self.project.form.refresh()
 
115
                
 
116
 
 
117
        #----------------------------------------------------------------------
 
118
        def AddFile(self, file_name):
 
119
                """Add a new file to the current folder"""
 
120
                
 
121
                import urllib
 
122
                
 
123
                # clean path to video
 
124
                file_name = urllib.unquote(file_name)
 
125
                
 
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):
 
128
                        return
 
129
                        
 
130
                # check if the path is a 'folder' and not a file
 
131
                if os.path.isdir(file_name):
 
132
                        
 
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)
 
136
                                
 
137
                                # only add files
 
138
                                if os.path.isfile(sub_file_path):
 
139
                                        
 
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:
 
142
 
 
143
                                                # inspect the media file and generate it's thumbnail image (if any)
 
144
                                                newFile = self.project.thumbnailer.GetFile(sub_file_path)
 
145
                                                
 
146
                                                # add to internal item collection
 
147
                                                if newFile:
 
148
                                                        self.items.append(newFile)
 
149
 
 
150
                else:
 
151
                        # inspect the media file and generate it's thumbnail image (if any)
 
152
                        newFile = self.project.thumbnailer.GetFile(file_name)
 
153
                
 
154
                        # add to internal item collection
 
155
                        if newFile:
 
156
                                self.items.append(newFile)
 
157
 
 
158
                return newFile
 
159
        
 
160
        #----------------------------------------------------------------------
 
161
        def ConvertFileToImages(self, file_name):
 
162
                """Add a new file to the current folder"""
 
163
                
 
164
                import urllib
 
165
 
 
166
                # clean path to video
 
167
                file_name = urllib.unquote(file_name)
 
168
 
 
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():
 
172
                                return True
 
173
                        
 
174
                # inspect the media file and generate it's thumbnail image (if any)
 
175
                newFile = self.project.thumbnailer.GetFile(file_name, all_frames=True)
 
176
                
 
177
                # update the location
 
178
                if newFile:
 
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")
 
182
        
 
183
                        # add to internal item collection
 
184
                        self.items.append(newFile)
 
185
 
 
186
                return
 
187
 
 
188
 
 
189
        def file_exists_in_project(self, file_name):
 
190
                """ Check if this file exists in this project """
 
191
                
 
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:
 
195
                                return True
 
196
                
 
197
                # didn't find a match
 
198
                return False
 
199
 
 
200
        #----------------------------------------------------------------------
 
201
        def get_file_path_from_dnd_dropped_uri(self, uri):
 
202
                """"""
 
203
 
 
204
                path = uri
 
205
                #path = urllib.url2pathname(uri) # escape special chars
 
206
                path = path.strip('\r\n\x00') # remove \r\n and NULL
 
207
                
 
208
                if os.name == 'nt':
 
209
                        path = path.replace("/", "\\")
 
210
                        path = path.replace("%20", " ")
 
211
 
 
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:')
 
219
                return path
 
220
 
 
221
 
 
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):
 
226
                                item.label = value
 
227
                                
 
228
                if refresh_tree == 1:
 
229
                        # Update the main form
 
230
                        self.project.form.refresh()
 
231
                                
 
232
                                
 
233
        def RemoveFile(self, filename):
 
234
                item = self.FindFile(filename)
 
235
                if item:
 
236
                        # mark project as modified
 
237
                        self.project.is_modified = True
 
238
                        
 
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:
 
244
                                                # delete clip
 
245
                                                track.clips.remove(clip)
 
246
                        # remove from file collection
 
247
                        self.items.remove(item)
 
248
                else:
 
249
                        #is this a folder?
 
250
                        item = self.FindFolder(filename)
 
251
                        if item:
 
252
                                # remove from file collection
 
253
                                self.items.remove(item)
 
254
 
 
255
  
 
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"""
 
261
                
 
262
                # loop through all files looking for the matching filename
 
263
                for file in self.items:
 
264
                        if isinstance(file, OpenShotFile):
 
265
                                name = file.name
 
266
                                
 
267
                                # check if file name matches this file
 
268
                                if file_name == name:
 
269
                                        return file
 
270
 
 
271
                                # split file name (if it's not found)
 
272
                                (dirName, fileName) = os.path.split(name)
 
273
                
 
274
                                # check if file name matches the basefile name
 
275
                                if file_name == fileName:
 
276
                                        return file
 
277
 
 
278
                
 
279
                # No file found
 
280
                return None
 
281
        
 
282
        
 
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"""
 
288
                
 
289
                # loop through all files looking for the matching filename
 
290
                for file in self.items:
 
291
                        if isinstance(file, OpenShotFile):
 
292
 
 
293
                                # check if file name matches this file
 
294
                                if unique_id == file.unique_id:
 
295
                                        return file
 
296
 
 
297
                # No file found
 
298
                return None
 
299
 
 
300
        
 
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):
 
306
                                name = folder.name
 
307
                                
 
308
                                if folder_name == name:
 
309
                                        return folder
 
310
        
 
311
        def ListFolders(self):
 
312
                """Return a list of any folders in the project"""
 
313
                
 
314
                folders = []
 
315
                for item in self.items:
 
316
                        if isinstance(item, OpenShotFolder):
 
317
                                folders.append(item.name)
 
318
                return folders
 
319
        
 
320
        def AddParentToFile(self, file_name, folder_name):
 
321
                file = self.FindFile(file_name)
 
322
                if file:
 
323
                        file.parent = folder_name
 
324
                        
 
325
        def RemoveParent(self, file_name, folder_name):
 
326
                file = self.FindFile(file_name)
 
327
                if file:
 
328
                        print "REMOVE PARENT"
 
329
                        file.parent = self.project.project_folder