6

I've implemented a way to upload video to youtube, etc. using multipart post, or save a video to the camera roll locally. However, with large videos I get watchdogged due to too large of a memory footprint, because currently I have to put the entire video in memory in order to post it or save it.

What are the steps I could take to break up a large video into manageable chunks?

akaru
  • 6,299
  • 9
  • 63
  • 102

1 Answers1

1

You can save your video to a file and use nsstream to read chunks of the video and send them, you'll have to keep some state to remember what u sent and what's left but it shouldn't be too bad to implement, for example

  BOOL done=FALSE;
   NSInputStream *stream=nil;

   NSString *myFile=@"..."; //your file path
   stream=[[NSInputStream alloc] initWithFileAtPath:myFile ];
  while(!done)
  {


   int chunkSize=500000; //500 kb chunks 
   uint8_t buf[chunkSize];
      //reads into the buffer and returns size read
   NSInteger size=[stream read:buf maxLength:chunkSize];

   NSLog(@"read %d bytes)", size);

    NSData * datum=[[NSData alloc] initWithBytes:buf length:size];
      //when we actually read something thats less than our chunk size we are done
   if(size<chunkSize)
     done=TRUE;
   //send data      
   }
Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Thanks, I understand I can stream the video, but how would this work with the SDK to save it to the Camera Roll? – akaru Jul 10 '11 at 19:14
  • Well, if you are downloading a file, you can append the data to a file as it comes (so you dont keep it all in memory) and then write it to the camera roll... – Daniel Jul 11 '11 at 17:21
  • I understand. However, my issue primarily lies with the call to save a local file to the camera roll, or to save that local file to the web. Either of those calls will result in the memory crash. – akaru Jul 13 '11 at 03:37
  • How are you saving to photo album? To save to photo album you only need the video URL, so not sure why it would be causing you problems... – Daniel Jul 13 '11 at 15:55
  • Just using the standard ALAssetsLibrary writeVideoAtPathToSavedPhotosAlbum api calls. – akaru Jul 15 '11 at 02:41
  • Can u post that code? What crash are you getting when you do that? I been saving pretty big videos to library using ALAssetsLibrary with no issues... – Daniel Jul 15 '11 at 16:37