如何使用C读取视频文件

时间:2019-02-07 12:04:10

标签: c video yuv

我有一个视频文件,想阅读并将结果显示在anathor文件中。

FILE *fp1,*fp2;

fp1=fopen("FOOTBALL_352x288_30_orig_01.yuv","rb");   
fp2=fopen("FOOTBALL_352x288_30_copy_01.yuv","wb");

while (feof(fp1))
{
  fread(1,sizeof(int),fp1);
  fwrite(fp1,sizeof(int),fp2);
}

fclose(fp1);
fclose(fp2);

3 个答案:

答案 0 :(得分:1)

您或多或少想要这样的东西:

#include <stdlib.h>
#include <stdio.h>

#define BUFFERSIZE 0x8000 // 32k buffer (adapt at will)

int main()
{
  FILE *fp1 = fopen("FOOTBALL_352x288_30_orig_01.yuv", "rb");

  if (fp1 == NULL)
  {
    // display error message to be written
    exit(1);
  }
  FILE *fp2 = fopen("FOOTBALL_352x288_30_copy_01.yuv", "wb");
  if (fp2 == NULL)
  {
    // display error message to be written
    exit(1);
  }    

  for (;;)   // loop for ever
  {
    char buffer[BUFFERSIZE];
    size_t bytesread = fread(buffer, 1, sizeof buffer, fp1);

    // bytesread contains the number of bytes actually read
    if (bytesread == 0)
    {
      // no bytes read => end of file
      break;
    }

    fwrite(buffer, bytesread, 1, fp2);
  }

  fclose(fp1);
  fclose(fp2);
}

免责声明:这是未经测试的代码,但是您应该了解一下。

仍有进一步改进的空间,尤其是文件末尾以外的实际读取错误(很少发生)未得到正确处理。

答案 1 :(得分:0)

这是我的解决方案,我已经测试过,并且似乎可以正常工作,您觉得家伙

#include <stdio.h>
#include <math.h>
#include <stdint.h>


	
int main(void) {
    FILE *fp1, *fp2;
 
	fp1= fopen("FOOTBALL_352x288_30_orig_01.yuv","rb");
	
	fp2= fopen("FOOTBALL_352x288_30_copy_02.yuv","wb");
   
 
	
int buffer;
while(!feof(fp1))
	{
		fread((void *)&buffer, sizeof(buffer),1,fp1);
		fwrite((void *)&buffer, sizeof(buffer),1,fp2);
		}
  

    fclose(fp1);
    fclose(fp2);
	
	return 0;
}

答案 2 :(得分:-1)

您想在!feof(fp1)时阅读。

fread读取缓冲区...您的缓冲区在哪里? fwrite从该缓冲区写入内容。

当然,您也不想读sizeof(int),而是使用具有指定大小的数据类型,例如uint8_t中的8位。

uint8_t buffer;

那你就可以做

fread((void *)&buffer, sizeof(uint8_t), 1, fp1);
fwrite((void *)&buffer, sizeof(uint8_t), 1, fp2);

当然,您还应该添加一些错误处理...

这也非常慢,因为您逐字节读取:)但这基本上是这样的。