avi2avippm.cc

  1 const char *help = "\
  2 progname: avi2avippm.cc\n\
  3 code2html: This program reads a video file and extracts ppm images and/or encode a new video file.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch) and Julien Tiphaigne\n";
  6 
  7 #include "ImageRgb.h"
  8 #include "rgbRawVideoFile.h"
  9 #include "ffmpegVideoFile.h"
 10 #include "vitcTimeCode.h"
 11 #include "ImageGray.h"
 12 #include "DiskXFile.h"
 13 #include "CmdLine.h"
 14 
 15 using namespace Torch;
 16 
 17 int main(int argc, char **argv)
 18 {
 19 	char *input_filename;
 20 	char *output_filename;
 21 	char *seek;
 22 	bool verbose;
 23 	bool saveasppm;
 24 	bool use_ffmpeg;
 25 	bool divx;
 26 	bool rgb;
 27 	bool vitc;
 28 	int nimages;
 29 	int h,m,s,f;
 30 	int bitrate;
 31 
 32 	// Construct the command line
 33   	CmdLine cmd;
 34 	cmd.setBOption("write log", false);
 35 	
 36   	// Put the help line at the beginning
 37   	cmd.info(help);
 38 
 39   	cmd.addText("\nArguments:");
 40   	cmd.addSCmdArg("input filename", &input_filename, "video filename");
 41   	cmd.addText("\nOptions:");
 42   	cmd.addBCmdOption("-vitc", &vitc, false, "extract vitc timecode");
 43   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 44   	cmd.addBCmdOption("-ffmpeg", &use_ffmpeg, false, "use ffmpeg to decode (raw RGB otherwise)");
 45   	cmd.addBCmdOption("-ppm", &saveasppm, false, "save images as ppm");
 46 	cmd.addBCmdOption("-divx", &divx, false, "save as divx AVI");
 47 	cmd.addBCmdOption("-rgb", &rgb, false, "save as raw RGB AVI");
 48 	cmd.addSCmdOption("-seek", &seek, "00:00:00:00", "seek video hh:mm:ss:ff");
 49   	cmd.addSCmdOption("-o", &output_filename, "output.avi", "output filename");
 50   	cmd.addICmdOption("-I", &nimages, -1, "number of images to process (all if -1)");
 51   	cmd.addICmdOption("-bitrate", &bitrate, 1000000, "bitrate for encoding (only with divx option)");
 52 
 53 	cmd.read(argc, argv);
 54 
 55 	if(strcmp(seek,""))
 56 		sscanf(seek, "%d:%d:%d:%d",&h,&m,&s,&f);
 57 	
 58 	if (divx && rgb) 
 59 	{
 60 		warning("Incompatible options divx and rgb");
 61 
 62 		return 1;	
 63 	}
 64 
 65 	VideoFile *video_in = NULL;
 66 	VideoFile *video_out = NULL;
 67 
 68 	if(use_ffmpeg) video_in = new ffmpegVideoFile();
 69 	else video_in = new rgbRawVideoFile();
 70 
 71 	if(divx) video_out = new ffmpegVideoFile();
 72 	if(rgb) video_out = new rgbRawVideoFile();
 73 
 74 	if(video_in->open(input_filename) == true)
 75 	{
 76 		int width = video_in->getwidth();
 77 		int height = video_in->getheight();
 78 		real fps = video_in->getframerate();
 79 		
 80 		if (divx || rgb) 
 81 		{
 82 			if (video_out->open(output_filename, "w") == false) 
 83 			{
 84 			   	delete video_in;
 85 				delete video_out;
 86 				return 1;
 87 			}
 88 		
 89 			video_out->setIOption("width", width);
 90 			video_out->setIOption("height", height);
 91 			video_out->setROption("framerate", fps);
 92 			if (divx)
 93 				video_out->setIOption("bitrate", bitrate);
 94 		}
 95 
 96 	
 97 		int nframes = video_in->getnframes();
 98 		Image *image = new ImageRgb(width, height);
 99 
100 		if(verbose)
101 		{
102 			print("width = %d\n", width);
103 			print("height = %d\n", height);
104 			print("nframes = %d\n", nframes);
105 			print("fps = %g\n", fps);
106 		}
107 		
108 		if (strcmp(seek,"")) 
109 		{
110 			if (video_in->seekmovie(h,m,s,f) <0)
111 			{
112 				video_in->close();
113 				if (divx || rgb) video_out->close();
114 
115 				delete video_in;
116 				if (divx || rgb) delete video_out;
117 
118 				return 1;
119 			}
120 
121 		}
122 
123 		if((nimages < 0) || (nimages > nframes)) nimages = nframes;
124 	
125 		vitcTimeCode *timecode = NULL;
126 		ImageGray *grayimage = NULL;
127 		
128 		if(vitc)
129 		{
130 			timecode = new vitcTimeCode(width, height);	
131 			grayimage = new ImageGray(width, height);
132 		}
133 		
134 		for(int i = 0 ; i < nimages ; i++)
135 		{   	
136 			print("%03d \r", i);
137 			if(video_in->read() == false) warning("Impossible to read frame %d\n", i);
138 		   	if(verbose) print("r");
139 
140 			if(vitc)
141 			{
142 				grayimage->copyFrom(width, height, video_in->pixmap, "rgb");
143 				timecode->reset(grayimage->pixmap);
144 			}
145 
146 			if (divx || rgb) 
147 			{
148 				video_out->write(video_in->pixmap);
149 				if(verbose) print("w");
150 			}
151 			
152 			if(saveasppm)
153 			{
154 				image->copyFrom(width, height, video_in->pixmap, "rgb");
155 				char str[250];
156 				sprintf(str, "image%06d.ppm", i);
157 				image->save(str);
158 
159 				if(vitc)
160 				{
161 					sprintf(str, "image%06d.pgm", i);
162 					//grayimage->save(str);
163 				}
164 			}
165 		
166 			real percent = 100.0 * (i+1) / nimages;
167 			if(vitc)
168 				//print(" %02d:%02d:%02d:%02d %03d%\r", timecode->hh(), timecode->mm(), timecode->ss(), timecode->ff(), (int) percent);
169 				print(" %02d:%02d:%02d:%02d %03d%\n", timecode->hh(), timecode->mm(), timecode->ss(), timecode->ff(), (int) percent);
170 			//else print(" %03d%\r", (int) percent);
171 		}
172 
173 		print("\n");
174 		
175 		video_in->close();
176 		if (divx || rgb) video_out->close();
177 
178 		if(vitc)
179 		{
180 			delete timecode;
181 			delete grayimage;
182 		}
183 		delete image;
184 	}
185 
186 	delete video_in;
187 	if (divx || rgb) delete video_out;
188 
189 	return(0);
190 }