Francesco Romani committed 03f84cf

[avi][tools] better maximum size detection for avisplit

When using avisplit with the "maximum size" option on videos with large
variable gaps between keyframes many files were over the limit, some by

I found that the maximum size was tested against the current filesize +
1MB. A frame size was taken and stored in the variable "bytes", but
that was just the single frame so it was very small, I imagine that plus
filesize never gave correct results so it was changed to arbitrary 1MB,
which would work on files with less than 1Mb between keyframes.

I added some code to loop through to the next keyframe (or eof) and get
the bytes between keyframes and test whether that plus filesize is over
the limit. I didn't notice any speed difference for all the extra

Patch courtesy of `Brad' <>

Comments (0)

Files changed (1)

   char *in_file=NULL;
-  long i, frames, bytes;
+  long i, frames, bytes, bytes_to_key, tmpreturn;
   uint64_t size=0;
   char *codec;
-  int j, n, key, k;
+  int j, n, m, key, k;
   int key_boundary=1;
       if(key && is_open && n && split_next) {
+        // loop through from this keyframe to the next to find out this chunks real size
+        // save bytes from first query
+        bytes_to_key = bytes;
+        //Loop until next keyframe
+        for(m=0; (!key || m==0); ++m) {
+          //video
+          tmpreturn = AVI_audio_size(in, n+m);
+          if (tmpreturn == -1) break;
+          bytes_to_key += tmpreturn;
+          //audio
+          tmpreturn =  AVI_read_frame(in, NULL, &key);
+          if (tmpreturn == -1) break;
+          bytes_to_key += tmpreturn;
+        }
+        //rewind to correct position, the last keyframe.
+        AVI_set_video_position(in, n);
         size = AVI_bytes_written(out);
         fsize = ((double) size)/MBYTE;
-        if((size + MBYTE) > (uint64_t)(chunk*MBYTE)) {
+        if((size + bytes_to_key) > (uint64_t)(chunk*MBYTE)) {
           // limit exceeded, close file