How to properly save a QTMovie after editing using QTKit?

1.2k views Asked by At

I am making minor edits to a QTMovie in an application using NSDocument architecture (such as adding a track, as shown below). After the edit, I want to save to the original file. However, I keep getting a 'file is busy' error. I assume this is due to some oversight I made in the handling of the files, or a failure in how I am using NSDocument. Any tips would be helpful! Here is (some of) the relevant code:

// open file that has the track I want to add to my movie
QTMovie* tempMovie = [QTMovie movieWithURL:outputFileURL error:nil];

// Use old API to add the track 
AddMovieSelection([movie quickTimeMovie], [tempMovie quickTimeMovie]);

// get the filename from the NSDocument
NSString *filename = [[self fileURL] path];
NSLog(@"writing to filename: %@", filename);

// FLATTEN the movie file so I don't get external references
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:1];
[attributes setObject:[NSNumber numberWithBool:YES] forKey:QTMovieFlatten];

// attempt to write
NSError *error;
//  this is where I get the "file is busy"
if (![movie writeToFile:filename withAttributes:attributes error:&error]) {
    NSLog(@"Error: %@", [error localizedDescription]);
    NSRunAlertPanel(@"Error", [error localizedDescription], nil, nil, nil);
}    

Do I have to first release the movie in my NSDocument? What is the "proper" way to do that? Keep in mind, I am not necessarily finished with this document, I am not closing it. I have just finished this operation, and I want the file on disk to reflect my changes. I would love to use [movie updateMovieFile], but that call doesn't seem to flatten the movie. I don't want any external references in my file.

2

There are 2 answers

0
pj4533 On BEST ANSWER

Turns out I just wasn't using the NSDocument architecture properly. When I changed it to use Save/SaveAs properly, this problem went away.

0
danyowdee On

I am not too familiar with the QuickTime C API, so I honestly can't tell you anything about what is going wrong there. Absolute guesswork: Maybe a call to EndMediaEdits is missing?
Though that shouldn't be required by AddMovieSelection, you said "[...] such as adding a track [...]". So maybe there is something else going on, like AddMediaSample or something similar?

That said, if you don't need to target anything below 10.5 and all you need to do is add some segment from another movie, you can achieve that without dropping down to the C API:

Have a look at

Do I have to first release the movie in my NSDocument?

You mean in order to write it to disk? No: That should even result in a crash.

The role of release is to handle memory-management. It doesn't have much to do with the busy-state of a file.