📄 avi2avippm.cc
字号:
const char *help = "\progname: avi2avippm.cc\n\code2html: This program reads a video file and extracts ppm images and/or encode a new video file.\n\version: Torch3 vision2.0, 2004-2005\n\(c) Sebastien Marcel (marcel@idiap.ch) and Julien Tiphaigne\n";#include "ImageRgb.h"#include "rgbRawVideoFile.h"#include "ffmpegVideoFile.h"#include "m2vtsLccRawVideoFile.h"#include "vitcTimeCode.h"#include "ImageGray.h"#include "jpegDiskXFile.h"#include "DiskXFile.h"#include "CmdLine.h"using namespace Torch;int main(int argc, char **argv){ char *input_filename; char *output_filename; char *seek; bool verbose; bool saveasppm; bool saveasjpeg; bool use_ffmpeg; bool use_lcc; bool divx; bool rgb; bool vitc; int nimages; int h,m,s,f; int bitrate; // Construct the command line CmdLine cmd; cmd.setBOption("write log", false); // Put the help line at the beginning cmd.info(help); cmd.addText("\nArguments:"); cmd.addSCmdArg("input filename", &input_filename, "video filename"); cmd.addText("\nOptions:"); cmd.addBCmdOption("-vitc", &vitc, false, "extract vitc timecode"); cmd.addBCmdOption("-verbose", &verbose, false, "verbose"); cmd.addBCmdOption("-ffmpeg", &use_ffmpeg, false, "use ffmpeg to decode (raw RGB otherwise)"); cmd.addBCmdOption("-lcc", &use_lcc, false, "use lcc M2VTS video format to decode (raw RGB otherwise)"); cmd.addBCmdOption("-ppm", &saveasppm, false, "save images as ppm"); cmd.addBCmdOption("-jpg", &saveasjpeg, false, "save images as jpg"); cmd.addBCmdOption("-divx", &divx, false, "save as divx AVI"); cmd.addBCmdOption("-rgb", &rgb, false, "save as raw RGB AVI"); cmd.addSCmdOption("-seek", &seek, "00:00:00:00", "seek video hh:mm:ss:ff"); cmd.addSCmdOption("-o", &output_filename, "output.avi", "output filename"); cmd.addICmdOption("-I", &nimages, -1, "number of images to process (all if -1)"); cmd.addICmdOption("-bitrate", &bitrate, 1000000, "bitrate for encoding (only with divx option)"); cmd.read(argc, argv); if(strcmp(seek,"")) sscanf(seek, "%d:%d:%d:%d",&h,&m,&s,&f); if (divx && rgb) { warning("Incompatible options divx and rgb"); return 1; } VideoFile *video_in = NULL; VideoFile *video_out = NULL; if(use_ffmpeg) video_in = new ffmpegVideoFile(); else if(use_lcc) video_in = new m2vtsLccRawVideoFile(); else video_in = new rgbRawVideoFile(); if(divx) video_out = new ffmpegVideoFile(); if(rgb) video_out = new rgbRawVideoFile(); if(video_in->open(input_filename) == true) { int width = video_in->getwidth(); int height = video_in->getheight(); real fps = video_in->getframerate(); if (divx || rgb) { if (video_out->open(output_filename, "w") == false) { delete video_in; delete video_out; return 1; } video_out->setIOption("width", width); video_out->setIOption("height", height); video_out->setROption("framerate", fps); if (divx) video_out->setIOption("bitrate", bitrate); } int nframes = video_in->getnframes(); Image *image = new ImageRgb(width, height); if(verbose) { print("width = %d\n", width); print("height = %d\n", height); print("nframes = %d\n", nframes); print("fps = %g\n", fps); } if (strcmp(seek,"")) { if (video_in->seekmovie(h,m,s,f) <0) { video_in->close(); if (divx || rgb) video_out->close(); delete video_in; if (divx || rgb) delete video_out; return 1; } } if((nimages < 0) || (nimages > nframes)) nimages = nframes; vitcTimeCode *timecode = NULL; ImageGray *grayimage = NULL; if(vitc) { timecode = new vitcTimeCode(width, height); grayimage = new ImageGray(width, height); } for(int i = 0 ; i < nimages ; i++) { print("%03d \r", i); if(video_in->read() == false) warning("Impossible to read frame %d\n", i); if(verbose) print("r"); if(vitc) { grayimage->copyFrom(width, height, video_in->pixmap, "rgb"); timecode->reset(grayimage->pixmap); } if (divx || rgb) { video_out->write(video_in->pixmap); if(verbose) print("w"); } if(saveasppm) { image->copyFrom(width, height, video_in->pixmap, "rgb"); char str[250]; sprintf(str, "image%06d.ppm", i); image->save(str); if(vitc) { sprintf(str, "image%06d.pgm", i); //grayimage->save(str); } } else if(saveasjpeg) { //image->copyFrom(width, height, video_in->pixmap, "rgb"); char str[250]; sprintf(str, "image%06d.jpg", i); //image->save(str); jpegDiskXFile *saver = new jpegDiskXFile(str, "w"); saver->writeHeader(width, height); saver->writePixmap(video_in->pixmap); delete saver; } real percent = 100.0 * (i+1) / nimages; if(vitc) //print(" %02d:%02d:%02d:%02d %03d%\r", timecode->hh(), timecode->mm(), timecode->ss(), timecode->ff(), (int) percent); print(" %02d:%02d:%02d:%02d %03d%\n", timecode->hh(), timecode->mm(), timecode->ss(), timecode->ff(), (int) percent); //else print(" %03d%\r", (int) percent); } print("\n"); video_in->close(); if (divx || rgb) video_out->close(); if(vitc) { delete timecode; delete grayimage; } delete image; } delete video_in; if (divx || rgb) delete video_out; return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -