FFmpeg Image/Video/Audio Processing


按帧截图, 图片合成视频, 视频格式转换, 视频拼接:

video_file = 'Cartier.flv'
cmd_probe = "ffprobe -hide_banner -v quiet -print_format json -show_format -show_streams -i %s"
cmd_frame = "ffmpeg -i %s -y -s 1280x960 -ss %s -frames 1 %s"
vinfo =[ i.strip() for i in os.popen3(cmd_probe % video_file)[2].read().splitlines() ]

合成视频流


用 图片序列+音频 合成

MP3:/home/ffmpeg_data/001.mp3、/home/ffmpeg_data/002.mp3
ffmpeg -r 0.5 -i 001.jpg 001.mp3 -vcodec mpeg4 001.mp4
ffmpeg -r 0.5 -i 001.jpg 001.mp3 -vcodec mpeg4 001.mp4
# 或将需要合并的视频文件写入 inputs.txt( 一行一个), 接着执行:
ffmpeg -y -f concat -safe 0 -i inputs.txt -c copy output.mp4

ffprobe提取视频信息


  1. 取得JSON格式视频/音频信息( 帧一级高精度的Stream 字段, 格式字段)
ffprobe  -hide_banner  -v quiet -print_format json -show_format -show_streams  -i  VIDEO_FILE/AUDIO_FILE
-count_frames 选项会在结果里加入每个 Stream 总帧数字段信息: "nb_read_frames" ,
但因需要处理每一帧,会影响调用执行的性能;
-show_frames 选项会在结果里加入frames帧信息(所有Stream的所有帧),
但因需要处理每一帧,会影响调用执行的性能;
  1. 提取TEXT格式视频/音频信息
Abael:flvs abael$ ffprobe  -hide_banner   -i  Cartier.flv
Input #0, flv, from 'Cartier.flv':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
encoder : Lavf58.12.100
Duration: 00:00:31.45, start: 0.057000, bitrate: 888 kb/s
Stream #0:0: Video: h264 (High), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 1k tbn, 50 tbc
Stream #0:1: Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s

TEXT格式信息可以取得Duration(时长), start(开始时间), fps(frame per second, 视频 Stream 的帧率).

  • 截取视频的1帧图片: 例如在0.0 时间截取一帧图片:

    ffmpeg -i /www/data/nginx/rtmp/flvs/CartierAD.flv -y -s 1280x960 -ss 0.0 -frames 1 ./bg.jpg
  • 拆分出视频的全部帧图片:

    ffmpeg -i video.avi frames_%05d.jpg

    导出结果 用 frame_ 为前缀,以5位数字为编号(不够前边用0补全),

    至于frames_%Nd.jpg保留几位数字长度, 可以先用 ffprobe 得到视频/音频 的总帧数来确定;

裁剪/合并/拼接 视频


  1. FFMPEG 裁剪视频:

    ffmpeg -ss START -t DURATION -i INPUT -vcodec copy -acodec copy OUTPUT

    -vcode copy和-acodec copy 表示使用的视频和音频的编码格式,copy表示原样拷贝视频源的。

  2. FFMPEG 合并视频文件:

    ffmpeg -i concat:"PART1.mpg|PART2.mpg" -vcodec copy -acodec copy OUTPUT.mpg

    PART1:要合并的第一个视频文件名,

    PART2:要合并的第二个视频文件名,

    OUTPUT:合并后的视频文件名

  3. FFMPEG 转换视频格式

    ffmpeg -i INPUT -f OUTPUT_FOMRMAT OUTPUT

    例如:ffmpeg -i /tmp/a.avi -f mpeg ./result.mpg

  4. 图片 和 音频文件 合成视频:

    • 照片(要提前准备好的):

      /home/ffmpeg_data/001.jpg、/home/ffmpeg_data/002.jpg
    • MP3(要提前准备好的):

      /home/ffmpeg_data/001.mp3、/home/ffmpeg_data/002.mp3
    • 用FFmpeg转换:

      ffmpeg -r 0.5 -i 001.jpg 001.mp3 -vcodec mpeg4 001.mp4

      也可以将需要合并的视频文件 放写在 inputs.txt( 一行一个), 接着执行:

      ffmpeg -y -f concat -safe 0 -i inputs.txt -c copy output.mp4
  5. 图片 合成 视频:

    ffmpeg [-start_number START_NUMBER] -i img/frames_%05d.jpg -vcodec OUTPUT_VIDEO_CODEC test.avi

    例如

    ffmpeg -i img/frames_%05d.jpg -vcodec mpeg4 test.avi

    • -vodec视频编码格式,所有ffmpeg支持的格式都可以,具体可以ffmpeg -codecs查看。
    • -i输入文件名,上例意为读取img文件夹文件名frame_ 为前缀 而且 后接五位数字jpg 图片序列
    • 报错解决: Could find no file with path ‘img/frames_%05d.jpg’ and index in the range 0-4

      这是因为ffmpeg默认图片编号从0开始的。即如果找不到 frames_00000.jpg报错

      指定读取图片文件名的起始编号为:

      ffmpeg -start_number 345 -i img/frames_%05d.jpg -vcodec mpeg4 test.avi
    • 注意:
      • 读取图片文件序列时会顺序读取,若出现编号中断,视频会就此结束
      • 默认图片文件名从0开始编号读取。如不存在frames_00000.jpg文件则报错.
      • 还可以通过:

FFmpeg CommandLine Arguments and Parameters

Video options:


-vframes number     **set the number of video frames to output**
-r rate **set frame rate (Hz value, fraction or abbreviation)**
-fpsmax rate **set max frame rate (Hz value, fraction or abbreviation)**
-s size **set frame size (WxH or abbreviation)**
-aspect aspect **set aspect ratio** (4:3, 16:9 or 1.3333, 1.7777)
-display_rotation angle **set pure counter-clockwise rotation in degrees for stream(s)**
-display_hflip **set display horizontal flip for stream(s)** (overrides any display rotation if it is not set)
-display_vflip **set display vertical flip for stream(s)** (overrides any display rotation if it is not set)
-vn **disable video**
-vcodec codec **force video codec** ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff **set initial TimeCode value**.
-pass n **select the pass number** (1 to 3)
-vf filter_graph **set video filters**
-b bitrate **video bitrate (please use -b:v)**
-dn **disable data**

Subtitle options:

-s size             set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated preset

FFmpeg Time Duration

FFmpeg时间段( time duration )格式,两种:

  • [-][:]:[....]

    HH 小时, MM 分钟(最大两位数), SS 秒(最多两位数),

    m 小数秒(高精度, 十进制);
  • [-]<S>+[.<m>...]

    S 秒数, m 作小数秒(高精度, 十进制);;

上两种duration, 都可选前加-指示negative duration.

例如:

-13.567:      **negative** 13.567 seconds
12:03:45: 12hours 03minutes 45 seconds
23.189: 23.189 seconds
00:01:00: 60 seconds
60: 60 seconds

FFmpeg Global options

affect whole program instead of just one file:

  • -loglevel loglevel set logging level
  • -v loglevel **set logging level**
  • -report generate a report
  • -max_alloc bytes **set maximum size of a single allocated block**
  • -y **overwrite output files**
  • -n never overwrite output files
  • -ignore_unknown Ignore unknown stream types
  • -filter_threads **number of non-complex filter threads**
  • -filter_complex_threads **number of threads for -filter_complex**
  • -stats print progress report during encoding
  • -max_error_rate maximum error rate ratio of decoding errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.
  • -frames[:stream_specifier] framecount (output,per-stream)

    指定产出帧数: 设置产出视频的帧数 framecount .
  • -f fmt (input/output)

    指定文件格式:

    • 导入:会自动检测导入格式;
    • 产出:文件扩展名自动推导产出格式 ( 因此这个 -f fmt 选项只在必要时使用. )
    • FFmpeg 支持的所有 fmt 格式可以查看执行: ffmpeg -formats
  • -ss position (input/output)

    指定时间起点:

    • -i 参数的前面是作为导入设置, 从导入文件快进到指定position.

      注️意:

      • 多数文件不真正支持seek, ffmpeg 会快进到 position 之前最接近的seek point.
      • 转码(transcoding)时并启用选项 -accurate_seek(默认), 则解码并丢弃 前置seek point 和 position 之间的帧.
      • 流复制(stream copy) 时并启用选项 -noaccurate_seek (默认), 则保留seek point 和 position 之间的帧.
    • -i 参数的后面是作为产出选项(放在 output url 之前);

      解码读入文件并丢弃导入, 直到产出流的 timestamp 到达这个指定的 position.
  • -sseof position (input/output)

    类似 "-ss" 选项,但时间点相对于 eof(end of file). 0 表示 EOF, 负数表示文件的stream上.
  • -to position (input/output)

    指定时间终点.

    • 在 写产出文件 / 读导入文件 到达指定时间终点 position后停止. ( ffmpeg Time duration 格式)
    • -to -t 两个选项只能两选一,且 -t优先级更高.
  • -t duration (input/output)

    指定时长.

    • 在 "-i" 的前面,作为导入设置, 指定只从导入文件读取的数据时间长度.
    • 在 "-i" 的后面(output url前),作为产出设置, 指定只写指定时长的数据,就停止.
    • -to -t 两个选项只能两选一,且 -t优先级更高
  • -fs limit_size (output)

    Set the file size limit, expressed in bytes.

    No further chunk of bytes is written after the limit is exceeded.

    The size of the output file is slightly more than the requested file size.
  • -itsoffset offset (input)

    指定导入时间偏移. offsetffmpeg time duration 格式

    offset 被添加到导入文件的timestamps(时间戳).

    指定positive offset 表示 streams 被 delayed 到指定 offset 的时间.
  • -timestamp date (output)

    指定录制时间戳

    在 container(数据容器, 输出stream到存储的格式容器)上 设置录制 timestamp.

FFmpeg Per-file main options:

-f fmt              **force format**
-c codec **codec name**
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] **set metadata information of outfile from infile**
-t duration **record or transcode "duration" seconds of audio/video**
-to time_stop **record or transcode stop time**
-fs limit_size **set the limit file size in bytes**
-ss time_off **set the start time offset**
-sseof time_off **set the start time offset relative to EOF**
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string **add metadata**
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number **set the number of frames to output**
-filter filter_graph **set stream filtergraph**
-filter_script filename **read stream filtergraph description from a file**
-reinit_filter **reinit filtergraph on input parameter changes**
-discard discard
-disposition disposition

FFmpeg 常用到PCM格式


 DE s16be           PCM signed 16-bit big-endian
DE s16le PCM signed 16-bit little-endian
DE s24be PCM signed 24-bit big-endian
DE s24le PCM signed 24-bit little-endian
DE s32be PCM signed 32-bit big-endian
DE s32le PCM signed 32-bit little-endian
DE s8 PCM signed 8-bit
DE f32be PCM 32-bit floating-point big-endian
DE f32le PCM 32-bit floating-point little-endian
DE f64be PCM 64-bit floating-point big-endian
DE f64le PCM 64-bit floating-point little-endian
DE mulaw PCM mu-law
DE u16be PCM unsigned 16-bit big-endian
DE u16le PCM unsigned 16-bit little-endian
DE u24be PCM unsigned 24-bit big-endian
DE u24le PCM unsigned 24-bit little-endian
DE u32be PCM unsigned 32-bit big-endian
DE u32le PCM unsigned 32-bit little-endian
DE u8 PCM unsigned 8-bit

ffmpeg 的解码编码格式

SciTech-AV-Video-DVP(Digital Video Processing)-CV/CG-ffmpeg-剪切/格式转换 & 视频拆帧图 & 图片合成视频 & 拼接 & time duration 格式 & 时间 起始点 时长 及 帧数设置的更多相关文章

  1. Digital Image Processing 学习笔记3

    第三章 灰度变换与空间滤波 3.1 背景知识 3.1.1 灰度变换和空间滤波基础 本章节所讨论的图像处理技术都是在空间域进行的.可以表示为下式: $$g(x, y) = T[f(x,y)]$$ 其中$ ...

  2. 解决:信息中插入avi格式的视频时,提示“unsupported video format”

    [测试步骤]:新建信息,添加AVI格式的视频 [测试结果]:添加时弹出提示"unsupported video format" 该问题主要提现在手机彩信视频附件不支持该AVI格式的 ...

  3. Video标签播放视频?谷歌浏览器?safari?? 谷歌浏览器播放不了mp4格式的视频的原因

    webm格式和mp4格式,判断了浏览器能否支持的视频类型后,给了一个if判断,如果是支持mp4格式,就返回视频后缀mp4,如果是webm,就返回后缀webm.结果,在谷歌浏览器中播放不了,为什么我指定 ...

  4. 关于AXI4-Stream to Video Out 和 Video Timing Controller IP核学习

    关于AXI4-Stream to Video Out 和 Video Timing Controller IP核学习 1.AXI4‐Stream to Video Out Top‐Level Sign ...

  5. vue / js使用video获取视频时长

    项目中遇到上传视频功能,需要有预览和获取视频时长功能,因之前使用upload(有需要的话可以参考下我之前的文章),这里就不赘述,直接用来上传视频,不过在上传之前和上传成功后的钩子里,获取不到时长: 没 ...

  6. 在MUI框架中使用video.js插件,并在暂停的时候利用Asp.net将观看时长保存到sqlserver数据库

    本次保存数据的情况有三种: 在视频播放的时候点击暂停,将本视频的进度保存到数据库 利用mui内部的控件,返回上一页操作时,进行保存 安卓手机触发返回键的时候,进行保存 示例一: 在video标签上面添 ...

  7. ffmpeg+nginx+video实现rtsp流转hls流,通过H5查看监控视频

    一.FFmpeg下载:http://ffmpeg.zeranoe.com/builds/ 下载并解压FFmpeg文件夹,配置环境变量:在“Path”变量原有变量值内容上加上d:\ffmpeg\bin, ...

  8. Digital image processing(数字图像处理)

    In computer science, digital image processing is the use of computer algorithms to perform image pro ...

  9. 信号处理的好书Digital Signal Processing - A Practical Guide for Engineers and Scientists

    诚心给大家推荐一本讲信号处理的好书<Digital Signal Processing - A Practical Guide for Engineers and Scientists>[ ...

  10. JS的video获取时长,出现问题汇总

    <video id="my_video_1" controls="controls" style=" width: 700px; height: ...

随机推荐

  1. Golang 版本导致的容器运行时问题

    问题现场 用户反馈安装了某个 containerd 版本的节点无法正常拉起容器,业务场景是在 K8S Pod 里面运行一个 Docker,在容器里面通过 docker 命令再启动新的容器. 报错信息如 ...

  2. Vue ElementUI 树表格

    树表格做懒加载-点击小箭头走接口 children为[]则使用hasChildren的true/false来判断是否有子节点,另,如果要做点击小箭头走接口必须加lazy及load <el-tab ...

  3. DOC,PDF,PPT文件转换为HTML代码记录

    pom文件引入 <repositories> <repository> <id>com.e-iceblue</id> <url>http:/ ...

  4. 百图生科:基于 JuiceFS 构建生命科学大模型存储平台,成本降 90%

    百图生科(BioMap)由百度创始人李彦宏先生联合创立,专注于生命科学领域的人工智能技术.公司推出了全球最大的生命科学 AI 基础模型 xTrimo V3,拥有 2100 亿参数,覆盖蛋白质.DNA. ...

  5. 航空货运系统总结性Blog

    前言 本次题目集以航空运送货物为背景,设计航空货物管理系统,主要考察对类设计的把握是否合理还有对继承和多态的使用,能否设计出符合标准的类,是否充分理解对面向对象六大设计原则(SRP,OCP,LSP,D ...

  6. L2-4、选择微调还是提示工程?企业级内容生成的最佳实践

    一.Prompt 工程与模型微调的本质区别 Prompt 工程的特点 Prompt 工程是通过精心设计输入提示来引导大语言模型生成所需输出的技术.它不改变模型的基本参数,而是利用现有模型能力. 工作原 ...

  7. Mysql高级操作(select嵌套,多表JOIN)

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  8. pip安装模块提示Command "python setup.py egg_info" failed with error code 1

    报错详情: [root@k8s001 ~]# pip install kubernetes Collecting kubernetes Using cached https://files.pytho ...

  9. ChatMoney,分析梦境的大师

    本文由 ChatMoney团队出品 作为一个爱幻想爱做白日梦的 i人,我常常就在想,什么时候能利用Al来帮助我找回一些被遗忘的.或者模糊不清的记忆? 有没有可能进入别人的梦境里瞧一瞧? 为什么世界上还 ...

  10. 在MySQL中悲观锁及乐观锁的应用

    本文由 ChatMoney团队出品 在数据库管理系统中,锁机制是保证数据一致性和并发控制的重要手段.MySQL,作为广泛使用的数据库系统之一,提供了多种锁策略来处理并发访问时可能引发的数据不一致性问题 ...