前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >NodeJS使用FFMPEG获取视频封面

NodeJS使用FFMPEG获取视频封面

作者头像
码客说
发布2022-12-22 14:11:13
2.1K0
发布2022-12-22 14:11:13
举报
文章被收录于专栏:码客码客

前言

大多数获取视频的封面都是使用FFMpeg获取视频的第一帧,但是很多视频第一帧是纯黑的,我们就要取后面的帧,这时候我们就要知道视频本身有多长。

获取视频时长

容器时长(container duration)的获取方法:

ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -i a.mp4

音视频流时长(stream duration)的获取方法:

ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 a.mp4

一个媒体文件里边有多个音视频流,各个流的时长也未必一样,一般播放器会以video stream的时长作为播放时长。

生成封面

ffmpeg -i a.mp4 -y -f image2 -ss 2 -frames 1 a001.jpg

方式2

ffmpeg -i a.mp4 -y -f image2 -ss 2 -t 0.001 a002.jpg

-ss 从几秒开始

NodeJS调用

let cp = require('child_process');

const execGetSec = (pathFile) => {
  const cmd = `ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 ${pathFile}`
  console.log('当前指令:', cmd)
  cp.exec(cmd, (err, stdout, errout) => {
    if (!err) {
      console.log('结果:', stdout)
    }
  })
}

const execJpg = (pathFile, saveFilePath) => {
  const cmd = `ffmpeg -i ${pathFile} -y -f image2 -ss 2 -frames 1 ${saveFilePath}`
  console.log('当前指令:', cmd)
  cp.exec(cmd, (err, stdout, errout) => {
    if (!err) {
      console.log(`${saveFilePath} success...`)
    }
  })
}

execGetSec("D:\\Video\\a.mp4");
// execJpg("D:\\Video\\a.mp4", "D:\\Video\\a002.jpg");

封装

let cp = require('child_process');

const execGetSec = (pathFile) => {
  return new Promise(((resolve) => {
    const cmd = `ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 ${pathFile}`
    cp.exec(cmd, (err, stdout, errout) => {
      if (!err) {
        resolve(parseInt(stdout))
      } else {
        resolve(0);
      }
    })
  }))
}

const execJpg = (pathFile, saveFilePath) => {
  return new Promise(((resolve, reject) => {
    const cmd = `ffmpeg -i ${pathFile} -y -f image2 -ss 2 -frames 1 ${saveFilePath}`
    cp.exec(cmd, (err, stdout, errout) => {
      if (!err) {
        resolve(saveFilePath);
      } else {
        resolve("")
      }
    })
  }))
}

const execJpgByTime = (pathFile, saveFilePath) => {
  return new Promise((async (resolve, reject) => {
    let sec = await execGetSec(pathFile);
    let fromSec = 0;
    if (sec > 5) {
      fromSec = 5;
    } else {
      fromSec = sec / 2;
    }
    const cmd = `ffmpeg -i ${pathFile} -y -f image2 -ss ${fromSec} -frames 1 ${saveFilePath}`
    cp.exec(cmd, (err, stdout, errout) => {
      if (!err) {
        resolve(saveFilePath);
      } else {
        resolve("")
      }
    })
  }))
}

async function main() {
  let sec = await execGetSec("D:\\Video\\a.mp4");
  console.info(sec);

  let path = await execJpg("D:\\Video\\a.mp4", "D:\\Video\\a_001.jpg");
  console.info(path);

  let path2 = await execJpgByTime("D:\\Video\\a.mp4", "D:\\Video\\a_002.jpg");
  console.info(path2);
}

main();

导出/导入

let cp = require('child_process');

const execGetSec = (pathFile) => {
  return new Promise(((resolve) => {
    const cmd = `ffprobe -v error -select_streams v:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 ${pathFile}`
    cp.exec(cmd, (err, stdout, errout) => {
      if (!err) {
        resolve(parseInt(stdout))
      } else {
        resolve(0);
      }
    })
  }))
}

const execJpgByTime = (pathFile, saveFilePath) => {
  return new Promise((async (resolve, reject) => {
    let sec = await execGetSec(pathFile);
    let fromSec = 0;
    if (sec > 5) {
      fromSec = 5;
    } else {
      fromSec = sec / 2;
    }
    const cmd = `ffmpeg -i ${pathFile} -y -f image2 -ss ${fromSec} -frames 1 ${saveFilePath}`
    cp.exec(cmd, (err, stdout, errout) => {
      if (!err) {
        resolve(saveFilePath);
      } else {
        resolve("");
      }
    })
  }))
}

exports.execJpgByTime = execJpgByTime;

导入

const {
  execJpgByTime
} = require("./coverUtil.js")

CentOS安装ffmpeg

yum安装

首先更新系统。

sudo yum install epel-release -y
sudo yum update -y

安装Nux Dextop Yum 源

由于CentOS没有官方FFmpeg rpm软件包。但是,我们可以使用第三方YUM源(Nux Dextop)完成此工作。

CentOS 7

sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm

CentOS 6

sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm

安装FFmpeg 和 FFmpeg开发包

sudo yum install ffmpeg ffmpeg-devel -y

测试是否安装成功

ffmpeg
ffprobe

Docker内不要添加sudo

# 安装ffmpeg
RUN yum install epel-release -y
RUN yum update -y
RUN rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
RUN rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
RUN yum install ffmpeg ffmpeg-devel -y

编译安装

先下载源码包:

git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg

然后进入ffmpeg文件夹,依次执行下列语句,当然连起来也可以:

cd ffmpeg
./configure
make && make install

时间较长,不出意外会正常安装好。

但是因为configure时候没有指定路径,所以直接ffmpeg会提示找不到。

所以要将编译好的ffmpeg复制到bin目录即可:

cp ffmpeg /usr/bin/ffmpeg

然后检查版本。

ffmpeg -version
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-12-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 获取视频时长
  • 生成封面
  • NodeJS调用
  • 封装
  • 导出/导入
  • CentOS安装ffmpeg
    • yum安装
      • 编译安装
      相关产品与服务
      容器服务
      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档