extract frame

来源:互联网 发布:中国气象数据查询 编辑:程序博客网 时间:2024/06/08 04:34

extract frames every two seconds:(fps:29.97)

  1. ffmpeg[fps:30]: #30,#90,#150 [ffmpeg: select one frame at the center]

  2. ext.c [fps:29]: #2, #58, #118

#include <stdio.h>#include <string.h>#include <stdlib.h>#include <cv.h>#include <highgui.h>#define PNG_COMPRESSION 3CvSize FixedSize(CvSize size);int main (int argc, char **argv){  if(argc < 3){    fprintf(stderr,"usage:eframe VIDEO_FILE frame_interval(sec) OutputDir\n");    return -1;  }  CvCapture* capture;  IplImage *frame = NULL;  char *filename = argv[1];  float sec_interval_frame = atof(argv[2]);  char *dir = argv[3];  int fps = 0, width = 0, height = 0, nframe = 0;  int n = 0;  int param[2];  char video[100],s1[100],s2[100];  char *ptr;   param[0] = CV_IMWRITE_PNG_COMPRESSION;  param[1] = PNG_COMPRESSION;  if((capture = cvCaptureFromFile(filename)) == NULL){    fprintf(stderr,"Can't open file %s\n.",filename);    return -1;  }  fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);  width = cvGetCaptureProperty (capture, CV_CAP_PROP_FRAME_WIDTH);  height = cvGetCaptureProperty (capture, CV_CAP_PROP_FRAME_HEIGHT);  nframe = cvGetCaptureProperty (capture, CV_CAP_PROP_FRAME_COUNT);  if (fps == 0) {    fprintf(stderr,"Cannot get frame rate of the video file: %s.\n",filename);    return -1;  }  strcpy(video,dir);  strcat(video,"/");  if ((ptr = strrchr(filename,'/')) != NULL){    ptr++;  }else{    ptr = filename;  }  ptr = strtok(ptr,".");  strcat(video,ptr);  strcat(video,"_");  fprintf(stdout,"# %s,%d,%d\n",filename,fps,nframe);  while(1) {    frame = cvQueryFrame(capture);    if(frame == NULL) break;    if (n % (int)(fps*sec_interval_frame) == 1){      strcpy(s1,video);      sprintf(s2, "%08d", n);      strcat(s1, s2);      strcat(s1,".png");      cvSaveImage(s1, frame, param);      fprintf(stdout,"%d\n",n);    }    n++;  }  cvReleaseCapture(&capture);  if (n != nframe){    fprintf(stderr,"# Error: %d != %d\n",n,nframe);  }  return 0;}

两种方法最后得到的图像的大小也不一样,因为都进行了压缩.
http://www.howtogeek.com/203979/is-the-png-format-lossless-since-it-has-a-compression-parameter/
PNG is Compressed, but Lossless
The compression level is a trade-off between file size and encoding/decoding speed. To overly generalize, even non-image formats such as FLAC have similar concepts.
Different Compression Levels, Same Decoded Output
Although the file sizes are different due the the different compression levels, the actual decoded output will be identical. You can compare the MD5 hashes of the decoded outputs with ffmpeg using the MD5 muxer. This is best shown with some examples.

Create PNG Files

ffmpeg -i input -vframes 1 -compression_level 0 0.pngffmpeg -i input -vframes 1 -compression_level 100 100.png

By default, ffmpeg will use -compression_level 100 for PNG output.
A quick, sloppy test showed that 100 (the highest compression level) took roughly three times longer to encode and five times longer to decode than 0 (the lowest compression level) in this example.

0 0
原创粉丝点击