~openshot.code/openshot/main

« back to all changes in this revision

Viewing changes to main/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
 
import os, sys
20
 
import urllib
21
 
import gtk
22
 
import messagebox
23
 
import project
24
 
import uuid
25
 
 
26
 
# init the foriegn language
27
 
import language.Language_Init as Language_Init
28
 
 
29
 
########################################################################
30
 
class OpenShotFile:
31
 
        """The generic file object for OpenShot"""
32
 
 
33
 
        #----------------------------------------------------------------------
34
 
        def __init__(self, project=None):
35
 
                """Constructor"""
36
 
                
37
 
                # Add language support
38
 
                translator = Language_Init.Translator()
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 path of OpenShot
63
 
                path = os.path.dirname(os.path.abspath(sys.argv[0]))
64
 
                
65
 
                # get the thumbnail (or load default)
66
 
                try:
67
 
                        if self.thumb_location:
68
 
                                pbThumb = gtk.gdk.pixbuf_new_from_file(self.thumb_location)
69
 
                        else:
70
 
                                # Load the No Thumbnail Picture
71
 
                                pbThumb = gtk.gdk.pixbuf_new_from_file("%s/images/AudioThumbnail.png" % (path))
72
 
                except:
73
 
 
74
 
                                # Load the No Thumbnail Picture
75
 
                                pbThumb = gtk.gdk.pixbuf_new_from_file("%s/images/AudioThumbnail.png" % (path))
76
 
 
77
 
                # resize thumbnail
78
 
                return pbThumb.scale_simple(width, height, gtk.gdk.INTERP_BILINEAR)     
79
 
                
80
 
 
81
 
########################################################################
82
 
class OpenShotFolder:
83
 
        """The generic folder object for OpenShot"""
84
 
 
85
 
        #----------------------------------------------------------------------
86
 
        def __init__(self, project=None):
87
 
                """Constructor"""
88
 
                
89
 
                # Init the variables for the Folder Object
90
 
                self.name = ""            # short / friendly name of the folder
91
 
                self.location = ""        # file system location
92
 
                self.parent = None
93
 
                self.project = project
94
 
                
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
98
 
                self.items = []
99
 
                
100
 
                # this queue holds files that are currently being added. this prevents
101
 
                # duplicate files to be added at the same time
102
 
                self.queue = []
103
 
 
104
 
 
105
 
        #----------------------------------------------------------------------
106
 
        def AddFolder(self, folder_name, project=None):
107
 
                
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))
112
 
                        return
113
 
                newFolder = OpenShotFolder(project)             
114
 
                newFolder.name = folder_name
115
 
                
116
 
                self.items.append(newFolder)
117
 
                self.project.form.refresh()
118
 
                
119
 
 
120
 
        #----------------------------------------------------------------------
121
 
        def AddFile(self, file_name):
122
 
                """Add a new file to the current folder"""
123
 
                
124
 
                import urllib
125
 
                
126
 
                # clean path to video
127
 
                file_name = urllib.unquote(file_name)
128
 
                
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):
131
 
                        return
132
 
                        
133
 
                # check if the path is a 'folder' and not a file
134
 
                if os.path.isdir(file_name):
135
 
                        
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)
139
 
                                
140
 
                                # only add files
141
 
                                if os.path.isfile(sub_file_path):
142
 
                                        
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:
145
 
 
146
 
                                                # inspect the media file and generate it's thumbnail image (if any)
147
 
                                                newFile = self.project.thumbnailer.GetFile(sub_file_path)
148
 
                                                
149
 
                                                # add to internal item collection
150
 
                                                if newFile:
151
 
                                                        self.items.append(newFile)
152
 
 
153
 
                else:
154
 
                        # inspect the media file and generate it's thumbnail image (if any)
155
 
                        newFile = self.project.thumbnailer.GetFile(file_name)
156
 
                
157
 
                        # add to internal item collection
158
 
                        if newFile:
159
 
                                self.items.append(newFile)
160
 
 
161
 
                return newFile
162
 
        
163
 
        #----------------------------------------------------------------------
164
 
        def ConvertFileToImages(self, file_name):
165
 
                """Add a new file to the current folder"""
166
 
                
167
 
                import urllib
168
 
 
169
 
                # clean path to video
170
 
                file_name = urllib.unquote(file_name)
171
 
 
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():
175
 
                                return True
176
 
                        
177
 
                # inspect the media file and generate it's thumbnail image (if any)
178
 
                newFile = self.project.thumbnailer.GetFile(file_name, all_frames=True)
179
 
                
180
 
                # update the location
181
 
                if newFile:
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")
185
 
        
186
 
                        # add to internal item collection
187
 
                        self.items.append(newFile)
188
 
 
189
 
                return
190
 
 
191
 
 
192
 
        def file_exists_in_project(self, file_name):
193
 
                """ Check if this file exists in this project """
194
 
                
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:
198
 
                                return True
199
 
                
200
 
                # didn't find a match
201
 
                return False
202
 
 
203
 
        #----------------------------------------------------------------------
204
 
        def get_file_path_from_dnd_dropped_uri(self, uri):
205
 
                """"""
206
 
 
207
 
                path = uri
208
 
                #path = urllib.url2pathname(uri) # escape special chars
209
 
                path = path.strip('\r\n\x00') # remove \r\n and NULL
210
 
                
211
 
                if os.name == 'nt':
212
 
                        path = path.replace("/", "\\")
213
 
                        path = path.replace("%20", " ")
214
 
 
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:')
222
 
                return path
223
 
 
224
 
 
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):
229
 
                                item.label = value
230
 
                                
231
 
                if refresh_tree == 1:
232
 
                        # Update the main form
233
 
                        self.project.form.refresh()
234
 
                                
235
 
                                
236
 
        def RemoveFile(self, filename):
237
 
                item = self.FindFile(filename)
238
 
                if item:
239
 
                        # mark project as modified
240
 
                        self.project.is_modified = True
241
 
                        
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:
247
 
                                                # delete clip
248
 
                                                track.clips.remove(clip)
249
 
                        # remove from file collection
250
 
                        self.items.remove(item)
251
 
                else:
252
 
                        #is this a folder?
253
 
                        item = self.FindFolder(filename)
254
 
                        if item:
255
 
                                # remove from file collection
256
 
                                self.items.remove(item)
257
 
 
258
 
  
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"""
264
 
                
265
 
                # loop through all files looking for the matching filename
266
 
                for file in self.items:
267
 
                        if isinstance(file, OpenShotFile):
268
 
                                name = file.name
269
 
                                
270
 
                                # check if file name matches this file
271
 
                                if file_name == name:
272
 
                                        return file
273
 
 
274
 
                                # split file name (if it's not found)
275
 
                                (dirName, fileName) = os.path.split(name)
276
 
                
277
 
                                # check if file name matches the basefile name
278
 
                                if file_name == fileName:
279
 
                                        return file
280
 
 
281
 
                
282
 
                # No file found
283
 
                return None
284
 
        
285
 
        
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"""
291
 
                
292
 
                # loop through all files looking for the matching filename
293
 
                for file in self.items:
294
 
                        if isinstance(file, OpenShotFile):
295
 
 
296
 
                                # check if file name matches this file
297
 
                                if unique_id == file.unique_id:
298
 
                                        return file
299
 
 
300
 
                # No file found
301
 
                return None
302
 
 
303
 
        
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):
309
 
                                name = folder.name
310
 
                                
311
 
                                if folder_name == name:
312
 
                                        return folder
313
 
        
314
 
        def ListFolders(self):
315
 
                """Return a list of any folders in the project"""
316
 
                
317
 
                folders = []
318
 
                for item in self.items:
319
 
                        if isinstance(item, OpenShotFolder):
320
 
                                folders.append(item.name)
321
 
                return folders
322
 
        
323
 
        def AddParentToFile(self, file_name, folder_name):
324
 
                file = self.FindFile(file_name)
325
 
                if file:
326
 
                        file.parent = folder_name
327
 
                        
328
 
        def RemoveParent(self, file_name, folder_name):
329
 
                file = self.FindFile(file_name)
330
 
                if file:
331
 
                        print "REMOVE PARENT"
332
 
                        file.parent = self.project.project_folder