Robotsystem from Robot Group Leipzig: splitvideo.c Source File

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# splitvideo.c

Go to the documentation of this file.

    00001 /***************************************************************************
    00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
    00003  *    martius@informatik.uni-leipzig.de                                    *
    00004  *    fhesse@informatik.uni-leipzig.de                                     *
    00005  *    der@informatik.uni-leipzig.de                                        *
    00006  *                                                                         *
    00007  *   This program is free software; you can redistribute it and/or modify  *
    00008  *   it under the terms of the GNU General Public License as published by  *
    00009  *   the Free Software Foundation; either version 2 of the License, or     *
    00010  *   (at your option) any later version.                                   *
    00011  *                                                                         *
    00012  *   This program is distributed in the hope that it will be useful,       *
    00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
    00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
    00015  *   GNU General Public License for more details.                          *
    00016  *                                                                         *
    00017  *   You should have received a copy of the GNU General Public License     *
    00018  *   along with this program; if not, write to the                         *
    00019  *   Free Software Foundation, Inc.,                                       *
    00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
    00021  *                                                                         *
    00022  *   $Log: splitvideo.c,v $
    00023  *   Revision 1.3  2005/11/09 13:31:51  martius
    00024  *   GPL'ised
    00025  *
    00026  ***************************************************************************/
    00027  #include <stdlib.h>
    00028  #include <stdio.h>
    00029  #include <assert.h>
    00030
    00031
    00032 /* mencoder mf:// *.png -mf w=800:h=600:fps=100:type=png -ovc lavc -lavcopts
    00033    vcodec=mpeg4 -oac copy -o output.avi -ffourcc DX50 */
    00034
    00035 void mencoder(const char* filename, int w, int h){
    00036   char cmd[1024];
    00037   printf("Should I invoke mencoder (mpeg2, mprg4 (DIVX 5), none)? [2/4/N] ");
    00038   int k= getchar();
    00039   switch(k) {
    00040   case '2':
    00041     sprintf(cmd,"for F in %s*.ppm; do echo \"convert $F\"; convert \"$F\" \"${F%%ppm}sgi\"; rm \"$F\"; done", filename);
    00042     printf("%s\n", cmd);
    00043     system(cmd);
    00044     sprintf(cmd,"mencoder mf://%s*.sgi -mf w=%i:h=%i:fps=%i:type=sgi -ovc lavc -lavcopts vcodec=mpeg2video -oac copy -o %s.mpg", filename, w, h, 25, filename);
    00045     printf("%s\n", cmd);
    00046     system(cmd);
    00047     break;
    00048   case '4':
    00049     sprintf(cmd,"for F in %s*.ppm; do echo \"convert $F\"; convert \"$F\" \"${F%%ppm}sgi\"; rm \"$F\"; done", filename);
    00050     printf("%s\n", cmd);
    00051     system(cmd);
    00052     sprintf(cmd,"mencoder mf://%s*.sgi -mf w=%i:h=%i:fps=%i:type=sgi -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o %s.avi -ffourcc DX50", filename, w, h, 25, filename);
    00053     printf("%s\n", cmd);
    00054     system(cmd);
    00055     break;
    00056   default:
    00057     return;
    00058   }
    00059
    00060   printf("Should I cleanup? [y/N] ");
    00061   while((k= getchar()) <32);
    00062   switch(k) {
    00063   case 'J':
    00064   case 'j':
    00065   case 'Y':
    00066   case 'y':
    00067     printf("Remove all temporary files\n");
    00068     sprintf(cmd,"rm -f %s*.sgi;", filename);
    00069     system(cmd);
    00070     break;
    00071   default:
    00072     break;
    00073   }
    00074 }
    00075
    00076
    00077 int main(int argc, char *argv[])
    00078 {
    00079    if (argc!= 4)
    00080    {
    00081      fprintf(stderr,"Usage: %s file w h\n", argv[0]);
    00082      exit(1);
    00083    }
    00084
    00085    char *fname = argv[1];
    00086    int w = atoi(argv[2]);
    00087    int h = atoi(argv[3]);
    00088    assert(w>0);
    00089    assert(h>0);
    00090
    00091    FILE *f = fopen(fname,"rb");
    00092    if (!f)
    00093    {
    00094      fprintf(stderr,"Cannot open %s for reading\n", fname);
    00095      exit(2);
    00096    }
    00097
    00098    int framenr=0;
    00099    int chunk = w*h*3;
    00100
    00101    unsigned char *buf = new unsigned char [chunk];
    00102
    00103
    00104    int retval;
    00105    do
    00106    {
    00107      retval = fread(buf, chunk, 1, f);
    00108      if (retval)
    00109      {
    00110        char outname[128];
    00111        sprintf(outname, "%s%04d.ppm", fname, framenr);
    00112        FILE *g=fopen(outname,"wb");
    00113        if(!g) {
    00114          fprintf(stderr,"Cannot open file %s for writing", outname);
    00115          exit(1);
    00116        }
    00117        fprintf(g,"P6 %d %d 255\n", w, h);
    00118        fwrite(buf, chunk, 1, g);
    00119        fclose(g);
    00120        framenr++;
    00121      }
    00122    } while (retval);
    00123    printf("Wrote %d frames\n", framenr);
    00124    mencoder(fname, w, h);
    00125
    00126
    00127    return 0;
    00128  }
    00129