1

I'm an OS X programming newbie, I have to take a bunch of NSImages arriving from the internet at a varying interval and construct a progressive movie file out of it. For example the rate of arrival of the NSImage could be anywhere from milli-seconds up to 3 seconds apart.

  1. I'm wondering if this is the correct way to do it using QTKit, and how to calculate the frame rate since frames come in almost at random times.

  2. I'm also wondering about memory usage, will it try to keep the whole movie in memory before writing it at the end?

Here is code I pieced together from the web:

-(void)startRecording  
{
    NSDictionary *myDict = nil;
    myDict = [NSDictionary dictionaryWithObjectsAndKeys:@"mp4v",
              QTAddImageCodecType,
              [NSNumber numberWithLong:codecHighQuality],
              QTAddImageCodecQuality,
              nil];

    long long timeValue = 1;
    long timeScale      = 3;
    QTTime duration     = QTMakeTime(timeValue, timeScale);
    QTMovie *mMovie =[[QTMovie alloc] initToWritableFile:@"tmpfps.tmp" error:NULL];

    self.mMovie=mMovie;
    self.duration=duration;
    self.myDict=myDict;
}

-(void) addFrame:(NSImage*)imageFrame
{
    [mMovie addImage:imageFrame
         forDuration:self.duration
      withAttributes:self.myDict];
}

-(void) stopRecording
{
    myDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
                                         forKey:QTMovieFlatten];
    [self.mMovie writeToFile:@"my.mov" withAttributes:myDict];
    self.mMovie=nil;
}
Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
N S
  • 303
  • 4
  • 17

1 Answers1

0

It's been a while since I've worked with QTKit, but in the past I wrote an app that did something very similar to what you describe (took images coming in somewhat irregularly over the network and wrote them to a QuickTime movie).

  1. When you call -[QTMovie addImage:forDuration:withAttributes], the duration parameter is the duration of the image in the movie. QTKit will take care of duplicating it to create enough movie frames to cover the duration. In short, you don't need to worry about the frame rate directly, just the duration of each frame.

    You can can figure out the duration of the frame by keeping track of when it was received and when the last frame was received. Essentially, you add each image to the movie when the next image comes in, and use the time difference between the two images' receipt times as the duration. Of course, you have to do something slightly different to account for the last image, depending on how recording is stopped (explicitly by the user, via timeout, etc.).

  2. I'm not as sure of the answer to this, but I think you can assume that this is an implementation detail that you don't need to worry about. Where you've initialized the QTMovie object with a writable file, it may be true that you can call -updateMovieFile periodically to flush current in-memory changes to the disk. This would require testing, but as with all performance optimization, you shouldn't worry about it until testing and profiling indicates that there's a problem.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • Thanks very much. I'm trying to see how I can do time stamps and time deltas using the QTTime API, but I dont see any QTTime function I can use to record the current time, and then take the difference so I can use it with the addImage:duration:withAttributes method. – N S Feb 27 '13 at 03:02