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

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 的解码编码格式

posted @   abaelhe  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示