4

使用 FFMPEG 编辑视频

 8 months ago
source link: https://liqiang.io/post/edit-videos-with-ffmpeg-command-64584f81
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用 FFMPEG 编辑视频

@SOLUTION· 2023-05-07 15:41 · 53 min read

经常在观看视频或者处理视频的时候,总是有一些很简单的小需求,例如截取视频某一段时间的内容,或者将一个视频按照比赛场次分割成多个视频,因为这个需求太简单了,所以特地安装一个视频处理软件就不太划算,刚好,我知道有个开源的工具 FFMPEG 是许多视频处理软件的底层依赖,所以我就尝试学习如何用它处理一个简单的视频,本文就总结一下我用到的命令。



  1. [[email protected]]# yum install epel-release
  2. [[email protected]]# rpm -v --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
  3. [[email protected]]# rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
  4. [[email protected]]# yum install -y ffmpeg ffmpeg-devel

查看视频的信息

ffmpeg 可以简单直观地查看视频的信息,例如视频包含几个轨道的数据,视频轨有几条,音频轨有几条(这些信息都可以被提取视频和音频所使用):



  1. [[email protected]]# ffmpeg -i IMG_2026.MOV
  2. ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers
  3. ... ...
  4. Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'IMG_2026.MOV':
  5. Metadata:
  6. major_brand : qt
  7. minor_version : 0
  8. compatible_brands: qt
  9. creation_time : 2023-08-20T09:35:26.000000Z
  10. com.apple.quicktime.location.accuracy.horizontal: 67.713067
  11. com.apple.quicktime.location.ISO6709: +01.2753+103.8402+029.502/
  12. com.apple.quicktime.make: Apple
  13. com.apple.quicktime.model: iPhone 13 Pro Max
  14. com.apple.quicktime.software: 16.6
  15. com.apple.quicktime.creationdate: 2023-08-20T17:35:26+0800
  16. Duration: 00:17:02.83, start: 0.000000, bitrate: 10039 kb/s
  17. Stream #0:0[0x1](und): Video: hevc (Main 10) (hvc1 / 0x31637668), yuv420p10le(tv, bt2020nc/bt2020/arib-std-b67), 1920x1080, 9796 kb/s, 29.99 fps, 29.97 tbr, 600 tbn (default)
  18. Metadata:
  19. creation_time : 2023-08-20T09:35:26.000000Z
  20. handler_name : Core Media Video
  21. vendor_id : [0][0][0][0]
  22. encoder : HEVC
  23. Side data:
  24. DOVI configuration record: version: 1.0, profile: 8, level: 4, rpu flag: 1, el flag: 0, bl flag: 1, compatibility id: 4
  25. Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 195 kb/s (default)
  26. Metadata:
  27. creation_time : 2023-08-20T09:35:26.000000Z
  28. handler_name : Core Media Audio
  29. vendor_id : [0][0][0][0]
  30. Stream #0:2[0x3](und): Data: none (mebx / 0x7862656D) (default)
  31. Metadata:
  32. creation_time : 2023-08-20T09:35:26.000000Z
  33. handler_name : Core Media Metadata
  34. Stream #0:3[0x4](und): Data: none (mebx / 0x7862656D), 0 kb/s (default)
  35. Metadata:
  36. creation_time : 2023-08-20T09:35:26.000000Z
  37. handler_name : Core Media Metadata
  38. Stream #0:4[0x5](und): Data: none (mebx / 0x7862656D), 34 kb/s (default)
  39. Metadata:
  40. creation_time : 2023-08-20T09:35:26.000000Z
  41. handler_name : Core Media Metadata
  42. At least one output file must be specified


  1. [[email protected]]# cat filelist.txt
  2. file '1.mp4'
  3. file '2.mp4'
  4. file '3.mp4'
  5. file '4.mp4'
  6. [[email protected]]# ffmpeg -f concat -i filelist.txt -c copy final.mp4


  1. [[email protected]]# ffmpeg -ss 00:00:00 -t 00:08:40 -i IMG_2266.MOV -acodec copy -vcodec copy game3.mp4
  2. [[email protected]]# ffmpeg -ss 00:09:20 -t 00:18:00 -i IMG_2266.MOV -acodec copy -vcodec copy game4.mp4
  • -t 的意思是时长,例如第二句的意思就是从视频的 09:20 分开始,截取一段时长为 18:00 的视频,所以这里实际山视频在原视频的时间段为 09:20 ~ 27:20

提取各种素材



  1. [[email protected]]# ffmpeg -i video.mp4 -i audio.mp3 -c copy -map 0:v:0 -map 1:a:0 output.mp4

这个命令是从 video.mp4audio.mp3 中提取视频和音频,然后合并成 output.mp4

  • -map 0:v:0:提取第 0 个输入文件的第 0 个视频轨道
  • -map 1:a:0:提取第 1 个输入文件的第 0 个音频轨道

简单了当的方式:

[[email protected]]# ffmpeg -i filename.mp4 -vn -acodec copy music.mp3

这里的 -acodec copy 可以简单写成:-c:a copy

精准的控制
[[email protected]]# ffmpeg -i video.mp4 -b:a 192K -vn -acodec copy music.mp3
[[email protected]]# ffmpeg -i video.mp4 -map 0:s:0 output.srt

Unsafe file name

[[email protected]]# ffmpeg -ss 00:00:00 -t 00:08:40 -i IMG_2266.MOV -acodec copy -vcodec copy game3.mp4[concat @ 0x56054a0] Unsafe file name '/mnt/backupsystem/archive2/Videos/20151222/PRIVATE/AVCHD/BDMV/STREAM'/dev/fd/63: Operation not permitted

解决方式,加上 -safe 0

[[email protected]]# ffmpeg -ss 00:00:00 -t 00:08:40 -safe 0 -i IMG_2266.MOV -acodec copy -vcodec copy game3.mp4

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK