Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uploading or exporting a large video sucks memory and causes crash...how can I break it up?

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?

like image 404
akaru Avatar asked Jul 07 '11 00:07

akaru


1 Answers

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      
   }
like image 104
Daniel Avatar answered Oct 26 '22 22:10

Daniel