YUV处理工具:文件分割和帧率扩倍功能

  1. 1、隔帧分割和帧率扩倍
  2. 2、算法实现
    1. 2.1 C实现
    2. 2.2、ffmpeg方法
  3. 3、ffmpeg分段分割
  4. THE END!

Tags: YUV处理实用小工具

1、隔帧分割和帧率扩倍

功能说明: 实现YUV文件分割为两个子YUV,隔帧写入YUV,实现帧率扩倍功能。

2、算法实现

2.1 C实现

cutYUV.cpp

/*****************************************************
//Function: split  a yuv file to two sub yuv file, fps
// increase twice.
//Date: 2018-5-30
//Author: SoaringLee
//Modified:
*****************************************************/

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

int main(int argc, char** argv)
{
    unsigned char* y;
    FILE* fp_input, *fp_out1, *fp_out2;
    int width, height, picturesize, framenum = 0;

    printf("Usage: splitYUV.exe  srcyuv dstyuv1 dstyuv2 width height\n\n");

    if (argc < 5)
    {
        return -1;
    }


    width = atoi(argv[4]);
    height = atoi(argv[5]);

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

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

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

    picturesize = width*height * 3 / 2;//++YUV420

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

    while (fread(y, 1, picturesize, fp_input) == picturesize)
    {
        if (framenum % 2 == 0)
        {
            fwrite(y, 1, picturesize, fp_out1);
        }
        else
        {
            fwrite(y, 1, picturesize, fp_out2);
        }
        framenum++;
    }

    printf("[SplitYUV] split %s to %s and %s successfully!!!\n", argv[1], argv[2], argv[3]);

    free(y);
    y = NULL;
    fclose(fp_input);
    fclose(fp_out1);
    fclose(fp_out2);

    return 0;
}

2.2、ffmpeg方法

另外,还可以通过ffmpeg实现隔帧分割的功能:

ffmpeg -r 2 -s WxH -i rawbitstream.yuv -filter:v select="mod(n-1\,2)" \
-c:v rawvideo -r 1 -format rawvideo -pix_fmt yuv420p -an odd.yuv

ffmpeg -r 2 -s WxH -i rawbitstream.yuv -filter:v select="not(mod(n-1\,2))" \
-c:v rawvideo -r 1 -format rawvideo -pix_fmt yuv420p -an even.yuv

更多可以参考:
https://superuser.com/questions/573747/drop-every-even-or-odd-frames-using-ffmpeg

3、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

更多命令使用参考:

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

THE END!


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

文章标题:YUV处理工具:文件分割和帧率扩倍功能

字数:539

本文作者:Soaring Lee

发布时间:2018-05-28, 21:13:47

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

原始链接:https://soaringleefighting.github.io/2018/05/28/【Codecs系列】YUV处理工具:文件分割和帧率扩倍功能/

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

×

喜欢就点赞,疼爱就打赏

相册