mfsname.cc

  1 const char *help = "\
  2 progname: mfsname.cc\n\
  3 code2html: This program reads a  video file with timecode enocded in the frames and print the timecode-derived name to stdout.\n\
  4 \n\
  5   This program is designed to be called by a perl script to rename avi\n\
  6   according to a standard convention.  From within perl, it is called\n\
  7   as follows:\n\
  8 \n\
  9 	`mfsname $sessionDir/$dir/$filename` =~ /(^T.*_T.*$)/m;\n\
 10 \n\
 11   in order to filter out any junk debugging statements that various\n\
 12   libraries might print out before the actual name is printed.\n\
 13 \n\
 14 version: Torch3 vision2.0, 2002-2005\n\
 15 (c) Pierre Wellner (wellner@idiap.ch) and Sebastien Marcel (marcel@idiap.ch)\n";
 16 
 17 // system
 18 #include <sys/stat.h>
 19  
 20 // image
 21 #include "ImageRgb.h"
 22 #include "ImageGray.h"
 23 
 24 // video
 25 #include "ffmpegVideoFile.h"
 26 #include "vitcTimeCode.h"
 27 
 28 using namespace Torch;
 29 
 30 /*------------------------------------------------------------------------
 31  * return pointer to file extension including the "."
 32   ------------------------------------------------------------------------*/
 33 char *GetFileExtension(char *pz)
 34 {
 35   int i;
 36   
 37   if (pz == 0)
 38     return "";
 39 
 40   i = strlen(pz);
 41   while ((i >= 0) && (pz[i] != '.'))
 42     i--;
 43   
 44   if (i >= 0)
 45     return &pz[i];
 46   else
 47     return "";
 48 	 
 49 } // end GetFileExtension
 50 
 51 int main(int argc, char *argv[])
 52 {
 53 	char *filename_in = "";
 54 	char *pzExtension = NULL;
 55 	unsigned numberOfFrames = 0;
 56 	char pzNewName[1000] = "";
 57 	struct stat statbuf;
 58 
 59 	if (argc < 2)
 60 	{
 61 	    fprintf(stderr, "usage: %s <video avi filename>\n", argv[0]);
 62 	    printf("%s", help);
 63 	    return -1;
 64 	}
 65 	
 66 	filename_in = argv[1];
 67 	
 68 	if ( 0 != stat(filename_in, &statbuf))
 69 	{
 70 	    fprintf(stderr, "ERROR: bad filename \"%s\"\n", filename_in);
 71 	    exit (-1);
 72 	}
 73 
 74 	pzExtension = GetFileExtension(filename_in);
 75 
 76 	//
 77 	ffmpegVideoFile *movie = new ffmpegVideoFile();
 78 
 79 	movie->open(filename_in);
 80 	
 81 	numberOfFrames = movie->getnframes();
 82 	if (numberOfFrames <= 0)
 83 	{
 84 		fprintf(stderr, "ERROR: bad filename \"%s\"\n", filename_in);
 85 		delete movie;
 86 		exit (-1);
 87 	}
 88 	
 89 	if(movie->read() != true)
 90 	{
 91 		delete movie;
 92 		return -1;
 93 	}
 94 
 95 	int width = movie->getwidth();
 96 	int height = movie->getheight();
 97 	
 98 	ImageRgb *image = new ImageRgb(width, height);
 99 	ImageGray *grayimage = new ImageGray(width, height);
100 	vitcTimeCode *timecode = new vitcTimeCode(width, height);
101 
102 	grayimage->copyFrom(width, height, movie->pixmap, "rgb");
103 	timecode->reset(grayimage->pixmap);
104 	
105 	
106 	sprintf(pzNewName,"T%02d%02d%02d.%03d_T%02d%02d%02d.%03d",
107 		timecode->hh(),  // hours
108 		timecode->mm(),  // minutes
109 		timecode->ss(),  // seconds
110 		timecode->ff() * (1000 / 25),   // milliseconds from frames
111 		
112 		numberOfFrames / (25 * 60 * 60),      // hours
113 		(numberOfFrames / (25 * 60)) % 60,    // minutes
114 		(numberOfFrames / 25) % 60,           // seconds
115 		(numberOfFrames % 25) * (1000 / 25)   // milliseconds
116 		);
117 	
118 	
119 	// cleanup
120 	movie->close();
121 	delete image;
122 	delete grayimage;
123 	
124 	printf("%s\n", pzNewName);
125 	
126 	return 0;
127 	
128 } // end main()