[FFmpeg-devel] Image sequence file names

Robert Petka robert.petka at gmail.com
Wed Aug 24 22:02:17 CEST 2011


Hi all,
recently I`ve been solving issue mentioned in following ticket 
http://avcodec.org/trac/ffmpeg/ticket/247.

I had to solve this issue for my project, so I want to share a solution 
with you. I was able to compile this code on MinGW under windows, but I 
do not have opportunity to try it on other platforms. This solution is 
not maybe nicest, kind a brute-force, but seems to work (it is also fast).

static void get_directory_from_path(const char* inPath, char* dirPath)
{
     int length, i;
     length = strlen(inPath);

     for(i=length-1;i>=0;i--)
     {
         if(inPath[i] == '\\' || inPath[i] =='/')
             break;
     }

     strncpy(dirPath, inPath, i + 1);
     dirPath[i + 1] = '\0';
}

static int get_index_for_file(const char* path)
{
     int length, i, last_digit, first_digit, index;
     length = strlen(path);

     first_digit = -1;
     last_digit = -1;

     for(i=length-1;i>=0;i--)
     {
         if(last_digit == -1)
         {
             if(isdigit(path[i]))
                 last_digit = i;
         }
         else
         {
             if(isdigit(path[i]))
                 first_digit = i;
             else
                 break;
         }
     }

     index = -1;
     if(first_digit != -1)
     {
         sscanf(path + first_digit,"%d%*s",&index);
     }

     return index;
}

/* return -1 if no image found */
static int find_image_range(int *pfirst_index, int *plast_index,
                             const char *path)
{
     char buf[1024];
     int range, last_index, range1, first_index;

     /* find the first image */
     //ROBO: Find the first image in extended range
     DIR *dp;
     struct dirent *ep;
     char dirPath[MAX_PATH];

     first_index = -1;

     get_directory_from_path(path, dirPath);

     dp = opendir (dirPath);
     if (dp != NULL)
     {
         int min_index;
         min_index = INT_MAX;

         while (ep = readdir (dp))
         {
             int file_index;
             file_index = get_index_for_file(ep->d_name);

             if(file_index != -1)
             {
                 if(file_index < min_index)
                     min_index = file_index;
             }
         }

         closedir(dp);

         first_index = min_index;
     }

     if(first_index == -1)
     {
         *pfirst_index = *plast_index = 1;
         goto fail;
     }

     /* find the last image */
     last_index = first_index;
     for(;;) {
         range = 0;
         for(;;) {
             if (!range)
                 range1 = 1;
             else
                 range1 = 2 * range;
             if (av_get_frame_filename(buf, sizeof(buf), path,
                                       last_index + range1) < 0)
                 goto fail;
             if (!url_exist(buf))
                 break;
             range = range1;
             /* just in case... */
             if (range >= (1 << 30))
                 goto fail;
         }
         /* we are sure than image last_index + range exists */
         if (!range)
             break;
         last_index += range;
     }
     *pfirst_index = first_index;
     *plast_index = last_index;
     return 0;
  fail:
     return -1;
}



Regards,
Robert


More information about the ffmpeg-devel mailing list