ffmpeg常用操作大合集

转码

-vcodec 视频编码、-r 指定帧率、-s 指定分辨率、-b 指定比特率;

-acodec 音频编码、-ab 指定音频比特率、-ac 指定声道数;

高清视频设置 -crf 18 -preset slow -profile:v high -level 4.2

ffmpeg -i input.wmv -s 640x480 -b 500k -vcodec h264 -r 29.97 -acodec libfaac -ab 48k -ac 2 out.mp4

剪切

用 -ss 和 -t 选项, 从第 30 秒开始,向后截取 10 秒的视频,并保存:

ffmpeg -i input.wmv -ss 30 -c copy -to 40 output.wmv

合并

准备a.txt文件内容为

file 'a.mp4'
file 'b.mp4'

然后执行

ffmpeg -f concat -i a.txt -c copy c.mp4

倍速

setpts实现视频的倍速,atempo实现音频的倍速

ffmpeg -i a.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" b.mp4

抽帧

从0秒到1分钟,每隔5秒存一帧,文件格式是%3d.jpg -r指定帧率为0.2,就是5秒一帧 -q:v 2 高质量图片

ffmpeg -ss 00:00 -i a.mp4 -f image2 -r 0.2 -q:v 2 -t 01:00 %3d.jpg

抽取PCM

把视频或音频文件里的音频保存成PCM格式

ffmpeg -i d:\tmp\eee\a.mp4 -codec:a pcm_s16le -f s16le -ac 1 -ar 16000 a.pcm

播放PCM

ffplay -ar 44100 -channels 1 -f s16le -i a.pcm

加字幕

ffmpeg.exe -i bg.mp4 -vf "drawtext=fontfile='arial.ttf':fontsize=40:fontcolor=white:textfile=lyric.txt" -c:a copy out.mp4

图片转视频

ffmpeg.exe -loop 1 -t 10 -i background.jpg -c:v libx264 -pix_fmt yuv420p -tune stillimage -r 30 output.mp4

常用的filters

FFmpeg Filters Documentation

  1. 播放速度x2 setpts=0.5*PTS

  2. 缩放 scale=400:-1

  3. 一秒一帧 fps=1

视频抽帧然后拼成九宫格

#coding=utf8
import sys
import os
import math
import shutil
ffmpegPath= r"D:\ffmpeg"
filename = sys.argv[1]
fps = 1 #frame per second
imagesDir = os.path.splitext(filename)[0]
if not os.path.exists(imagesDir):
    os.mkdir(imagesDir)
extractCmd = "%s\\ffmpeg.exe -i %s -vf \"scale=400:-1, fps=1, drawtext=fontfile='arial.ttf':fontsize=40:fontcolor=white:text='%%\{pts\\:gmtime\\:0\:%%M\\\\\\:%%S\}':x=w-tw-10:y=h-th-10\" -f image2 -q:v 2 %s\\%%3d.jpg -y" % (ffmpegPath, filename, imagesDir)
print(extractCmd)
os.system(extractCmd)
imageCnt = len(os.listdir(imagesDir))


tileW = int(math.sqrt(imageCnt))
tileH = tileW
while tileW * tileH < imageCnt:
    tileH += 1


print(tileW, tileH)
tileImage = imagesDir + ".jpg"
tileCmd = "%s\\ffmpeg.exe -f image2 -i %s\\%%3d.jpg -vf \"tile=%dx%d\" \"%s\" -y" % (ffmpegPath, imagesDir, tileW, tileH, tileImage)
print(tileCmd)
os.system(tileCmd)

打印视频帧的信息

ffprobe.exe -show_frames -select_streams v out1.mp4 > c.txt

Ubuntu 安装ffmpeg

CompilationGuide/Ubuntu – FFmpeg

留言: