c - 如何使用 C 读取视频文件
问题描述
我有一个视频文件,想阅读它并在 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);
解决方案
你或多或少想要这样的东西:
#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);
}
免责声明:这是未经测试的代码,但您应该明白这一点。
仍有进一步改进的空间,尤其是文件结尾以外的实际读取错误(很少发生)没有得到正确处理。
推荐阅读
- blockchain - 如何仅从保管库查询中获取数据
- javascript - 为什么我需要覆盖prototype.constructor 值?
- php - SQLSTATE [42S02]:未找到基表或视图:1146 表“fresh_start.event”不存在(SQL:从“事件”中选择计数(*)作为聚合)
- javascript - React/Redux,从模型调用方法抛出 TypeError
- ios - 如何在我的 UIPageViewController.swift 中添加另一个页面?
- google-cloud-platform - Swagger 文档位置不存在
- python - 如何在熊猫系列中为 NaN 赋值?
- visual-studio - 从 Visual Studio 部署到 Azure Analysis Services 会重置数据源“路径”属性
- ansible - ansible的authorized_keys问题
- multithreading - 如何在 freeRTOS 中使用互斥锁同步线程?