YUV处理工具:文件剪切

  1. 1、YUV文件剪切
  2. 2、算法实现
    1. 2.1、方法一:C语言实现
    2. 2.2、方法二:采用ffmpeg命令行实现
  3. THE END!

Tags: YUV处理实用小工具

1、YUV文件剪切

功能说明:实现YUV文件剪切,可以设置开始帧和结束帧,裁剪得到任意帧数的YUV数据。

2、算法实现

2.1、方法一:C语言实现

#include "stdio.h"
#include "stdlib.h"
#include "string"

int main(int argc, char** argv)
{
    unsigned char* y;
    FILE* fp_input, *fp_out;
    int i, width, height, start, stop;
    int filelen, frames_no, picturesize, skip_size;
    char outname[100];

    if (argc < 6)
    {
        printf("Usage: extractYUV.exe  input.yuv  width height startframe stopframe\n\n");
        return -1;
    }


    width = atoi(argv[2]);
    height = atoi(argv[3]);
    start = atoi(argv[4]);
    stop = atoi(argv[5]);
    sprintf_s(outname,"%s_%d_%d.yuv",argv[1],start,stop);

    fp_input = fopen(argv[1], "rb");
    if (NULL == fp_input)
    {
        printf("open %s fail!\n",argv[1]);
        return -1;
    }

    fp_out = fopen(outname, "wb");
    if (NULL == fp_out)
    {
        printf("open %s fail!\n",argv[1]);
        return -1;
    }

    picturesize = width*height*3/2;
    fseek(fp_input,SEEK_END,SEEK_SET);
    filelen = ftell(fp_input);
    frames_no = filelen / picturesize;

    skip_size = start * picturesize;
    fseek(fp_input, skip_size,SEEK_SET);

    y = (unsigned char*)malloc(picturesize*sizeof(unsigned char*));

    for (i = 0; i < (stop - start); i++)
    {
        if (fread(y, 1, picturesize, fp_input) == picturesize)
        {
            fwrite(y, 1, picturesize, fp_out);
        }
    }

    printf("extractYUV ok!!!, total frames:%d\n",stop - start);

    free(y);
    y = NULL;
    fclose(fp_input);
    fclose(fp_out);

    return 0;
}

2.2、方法二:采用ffmpeg命令行实现

Extract some YUV frames from large yuv File

从第0帧开始截取30帧:

ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="gt(n\, -1)" -vframes 30 out30.yuv

或者:

ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="between(n\, 0\, 29)" out30.yuv

或者:

ffmpeg -r 1 -ss 0 -i input.yuv -vcodec copy -vframes 30 output.yuv

中间截取帧(截取从第30帧到第100帧):

ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="between(n\, 30\, 100)" out.yuv

根据时间截取帧(截取从第10秒到第20秒 ):

//Select only frames contained in the 10-20 time interval:
//select=between(t\,10\,20)
ffmpeg -s widthxheight -i input.yuv -c:v rawvideo -filter:v select="between(t\, 10\, 20)" out.yuv

更多ffmpeg命令使用可参考:

http://processors.wiki.ti.com/index.php/Open_Source_Video_Processing_Tools_-_MPlayer,_FFMpeg,_AviSynth,_MKVToolnix,_MP4Box#Downloads
http://ffmpeg.org/ffmpeg-filters.html#aselect_002c-select
https://lists.ffmpeg.org/pipermail/ffmpeg-user/2017-February/035335.html
https://www.bookstack.cn/read/other-doc-cn-ffmpeg/ffmpeg-doc-cn-40.md


THE END!


本博文只能阅读,谢绝转载,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2963033731@qq.com

文章标题:YUV处理工具:文件剪切

字数:531

本文作者:Soaring Lee

发布时间:2018-05-29, 15:13:47

最后更新:2021-06-14, 12:13:44

原始链接:https://soaringleefighting.github.io/2018/05/29/【Codecs系列】YUV处理工具:文件剪切功能/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏

相册