• ffmpeg转码使用硬件加速


    需求源于手机拍摄的视频,默认参数码率较大,拍摄的文件体积较大,不便于保存和转发。
    手机默认拍照的720P视频,默认码率达到4M,实际上转成1M就差不多了。
    FFmpeg默认的转码是使用软件解码,然后软件编码,速度慢,CPU使用率太高(基于100%)。

    本次测试
    硬件环境:

    CPU: Intel i5-6400 4核2.7GHz
    RAM: 8GB 
    SSD: Reeinno ST120GB
    GPU: Intel HD Graphics 530 芯片集成显卡

    软件环境:

    Windows 7 SP1 64bit
    FFmpeg version N-86175-g64ea4d1 Copyright (c) 2000-2017 the FFmpeg developers

    1. 默认参数

    ffmpeg.exe -i 20180206210632.mp4 -f mp4 20180206210632-1.mp4

    得到 fps 57, speed 1.97x, CPU 95+%

    Stream mapping:
    Stream #0:1 -> #0:0 (h264 (native) -> h264 (libx264)) # 这里看到编码都是使用libx264
    ------
    frame= 794 fps= 57 q=29.0 size= 5368kB time=00:00:27.56 bitrate=1595.6kbits/s speed=1.97x

    2. 编码使用硬件

    ffmpeg.exe -i 20180206210632.mp4 -vcodec h264_qsv -f mp4 20180206210632-2.mp4

    得到 fps 239, speed 8.34x, CPU 85+%

    Stream mapping:
    Stream #0:1 -> #0:0 (h264 (native) -> h264 (h264_qsv)) # 这里看到编码都是使用h264_qsv
    ------
    frame= 720 fps=239 q=-0.0 size= 2909kB time=00:00:25.10 bitrate= 949.4kbits/s speed=8.34x

    3. 解码也指定硬件解码

    ffmpeg.exe -c:v h264_qsv -i 20180206210632.mp4 -vcodec h264_qsv -f mp4 20180206210632-3.mp4

    得到 fps 219, speed 7.45x, CPU 60+%

    Stream mapping:
    Stream #0:1 -> #0:0 (h264 (h264_qsv) -> h264 (h264_qsv)) # 这里看到编码都是使用h264_qsv,但是没有完全使用硬件加速
    ------
    frame= 1867 fps=219 q=-0.0 size= 7869kB time=00:01:03.57 bitrate=1013.9kbits/s speed=7.45x

    4. 指定硬件加速qsv

    ffmpeg.exe -hwaccel qsv -c:v h264_qsv -i 20180206210632.mp4 -vcodec h264_qsv -f mp4 20180206210632-4.mp4

    运行这个报错

    Impossible to convert between the formats supported by the filter 'graph 0 input from stream 0:1' and the filter 'auto_scaler_0'
    Error reinitializing filters!
    Failed to inject frame into filter network: Function not implemented
    Error while processing the decoded data for stream #0:1
    [aac @ 000000000234a940] Qavg: 2433.237
    [aac @ 000000000234a940] 2 frames left in the queue on closing
    Conversion failed!

    后来经过大师兄FFmpeg微信群 李忠@Intel的提示,视频带有旋转信息,才发现手机拍摄的视频自带90°旋转。
    添加输入参数-noautorotate后为:

    ffmpeg.exe -hwaccel qsv -c:v h264_qsv -noautorotate -i 20180206210632.mp4 -vcodec h264_qsv -f mp4 20180206210632-5.mp4

    得到 fps 347, speed 11.7x, CPU 30+% 目前最好的效果了

    Stream mapping:
    Stream #0:1 -> #0:0 (h264 (h264_qsv) -> h264 (h264_qsv)) # 这里看到解码和编码都是使用h264_qsv
    ------
    frame= 5740 fps=347 q=-0.0 size= 24773kB time=00:03:12.77 bitrate=1052.8kbits/s speed=11.7x

    尝试过其他的软件,比如:
    HandBrake-1.0.7-x86_64-Win_GUI
    MediaCoder-x64-0.8.52.5920
    通过设置能达到编码使用Intel QSV,但是解码依然是CPU,并且使用率依然很高。

    尝试过Linux平台,发现硬件加速还提示缺失文件。下次再研究。

  • 相关阅读:
    2021/6/28
    2021/6/25
    IDEA快捷键
    maven的一些问题
    Maven安装
    2021/6/14
    nmcli 网络管理工具
    linux yum仓库配置
    linux 防火墙selinux ,firewalld, iptables
    linux root密码重置
  • 原文地址:https://www.cnblogs.com/zzugyl/p/8439060.html
Copyright © 2020-2023  润新知