参考文档:
官网
音视频处理工具ffmpeg
ffmpeg官方文档
音视频合成分割
-
基本用法
-
ffmpeg 处理视频音频
转化方式 | 编码 |
---|---|
转化格式 | ffmpeg -i out.mp4 -vcodec copy -acodec copy out.flv / ffmpeg -i out.ogv -vcodec h264 out.mp4 |
只抽取视频 | ffmpeg -i out.mp4 -an -vcodec copy out.h264 |
只提取音频 | ffmpeg -i out.mp4 -acodec copy -vn out.aac |
ffmpeg提取YUV数据 | ffmpeg -i input.mp4 -an -c:v rawvedio -pix_fmt uyv420p out.yuv |
ffmpeg 提取PCM数据 | ffmpeg -i input.mp4 -vn -ar 44100 -ac2 -f s16le out.pcm |
图片转视频 | ffmpeg -y -r 1 -i %d.jpg -vcodec libx264 pic.mp4 |
视频转图片 | ffmpeg -r 1 -i pic.mp4 frame_%02d.png |
剪切视频 | ffmpeg -ss 0:00:00 -t 0:00:15 -i Try.mp4 -vcodec copy -acodec copy little.mp4 |
抽取视频中音乐 | ffmpeg -i little.mp4 -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3 |
视频转gif图 | ffmpeg -i little.mp4 gif_test.gif |
视频+音频背景音,合成视频 | ffmpeg -i pic.mp4 -i sound.mp3 -t 10 -y composite_mp4.mp4 |
去掉背景音 | ffmpeg -i little.mp4 -c:v copy -an no-audio.mp4 |
音频合成 | ffmpeg -y -i "concat: test1.mp3 |
音频切分 | ffmpeg -y -i input.mp3 -ss 00:00:00 -t 00:00:05 outpath.mp3 |
加速四倍 | ffmpeg -i TheOrigin.mp4 -vf “setpts=0.25*PTS” UpTheOrigin.mp4 |
【参数说明】
-vcodec copy:视频编码处理方式
-acodec copy:音频编码处理方式
-t 时长
-y 是否覆盖同名文件
-ss 时间段表达式,例如 00:00:01 表示为视频第一秒
vframes 视频帧数
-i :指定要转换视频的源文件
-s : 视频转换后视频的分辨率
-r : 视频转换换的桢率(默认25桢每秒)
-b : 视频转换换的bit率
-ab : 音频转换后的bit率(默认64k)
-ac :制定转换后音频的声道
-ar :音频转换后的采样率
-
注意:用 -ss 和 -t 选项与 用 -t, 从第 30 秒开始,向后截取 10 秒的视频,并保存:ffmpeg -i input.wmv -ss 00:00:30.0 -c copy -t 00:00:10.0 output.wmv / ffmpeg -i input.wmv -ss 30 -c copy -t 10 output.wmv
达成相同效果,也可以用 -ss 和 -to 选项, 从第 30 秒截取到第 40 秒:ffmpeg -i input.wmv -ss 30 -c copy -to 40 output.wmv -
多个视频合并
$ cat mylist.txt
file ‘/path/to/file1’
file ‘/path/to/file2’
file ‘/path/to/file3’ $
ffmpeg -f concat -i mylist.txt -c copy output多个视频文件合并,更多时候,由于输入文件的多样性,需要转成中间格式再合成:
ffmpeg -i input1.avi -qscale:v 1
intermediate1.mpg ffmpeg -i input2.avi -qscale:v 1 intermediate2.mpg
cat intermediate1.mpg intermediate2.mpg > intermediate_all.mpg ffmpeg
-i intermediate_all.mpg -qscale:v 2 output.avi
视频分镜处理
1、基于哈希算法
def aHash(img):
# 缩放为8*8
plt.imshow(img)
plt.axis('off') #去掉坐标轴
#plt.show()
img = cv2.resize(img, (8, 8))
plt.imshow(img)
plt.axis('off') #去掉坐标轴
#plt.show()
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# s为像素和初值为0,hash_str为hash值初值为''
s = 0
hash_str = ''
# 遍历累加求像素和
for i in range(8):
for j in range(8):
s = s + gray[i, j]
# 求平均灰度
avg = s / 64
# 灰度大于平均值为1相反为0生成图片的hash值
for i in range(8):
for j in range(8):
if gray[i, j] > avg:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
2、基于差值感知算法
# 差值感知算法
def dHash(img):
# 缩放8*8
img = cv2.resize(img, (9, 8))
# 转换灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hash_str = ''
# 每行前一个像素大于后一个像素为1,相反为0,生成哈希
for i in range(8):
for j in range(8):
if gray[i, j] > gray[i, j + 1]:
hash_str = hash_str + '1'
else:
hash_str = hash_str + '0'
return hash_str
3、基于感知哈希算法
# 感知哈希算法(pHash)
def pHash(img):
# 缩放32*32
img = cv2.resize(img, (32, 32)) # , interpolation=cv2.INTER_CUBIC
# 转换为灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 将灰度图转为浮点型,再进行dct变换
dct = cv2.dct(np.float32(gray))
# opencv实现的掩码操作
dct_roi = dct[0:8, 0:8]
hash = []
avreage = np.mean(dct_roi)
for i in range(dct_roi.shape[0]):
for j in range(dct_roi.shape[1]):
if dct_roi[i, j] > avreage:
hash.append(1)
else:
hash.append(0)
return hash
4、基于RGB的直方图
# 通过得到RGB每个通道的直方图来计算相似度
def classify_hist_with_split(image1, image2, size=(256, 256)):
# 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
image1 = cv2.resize(image1, size)
image2 = cv2.resize(image2, size)
plt.imshow(image1)
#plt.show()
plt.axis('off')
plt.imshow(image2)
#plt.show()
plt.axis('off')
sub_image1 = cv2.split(image1)
sub_image2 = cv2.split(image2)
sub_data = 0
for im1, im2 in zip(sub_image1, sub_image2):
sub_data += calculate(im1, im2)
sub_data = sub_data / 3
return sub_data
5、基于单通道直方图
# 计算单通道的直方图的相似值
def calculate(image1, image2):
hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
plt.plot(hist1, color="r")
plt.plot(hist2, color="g")
#plt.show()
# 计算直方图的重合度
degree = 0
for i in range(len(hist1)):
if hist1[i] != hist2[i]:
degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
else:
degree = degree + 1
degree = degree / len(hist1)
return degree
【分帧比较视频的前后帧相似度,将相似度较低的帧提取出来】
for i in range(549):
img1 = cv2.imread('image/image{}.jpg'.format(i))
img2 = cv2.imread('image/image{}.jpg'.format(i+1))
n = classify_hist_with_split(img1, img2)
if (n[0]<0.51):
print('三直方图算法相似度:',n)
cv2.imwrite('shot1/image{}.jpg'.format(i+1),img2)
if n==1:
continue
#hash1= pHash(img1)
#hash2= pHash(img2)
#n = cmpHash(hash1, hash2)
#if (n>10):
#print('均值哈希算法相似度:',n)
#cv2.imwrite('shot/image{}.jpg'.format(i+1),img2)
注意:在三直方图计算相似度时可能出现n=1的情况,此时不能用n[0]与常数进行比较,需要新增条件if。
测试结果,哈希算法设值为10左右时,效果较好,能够保留同场景中的人物变化较大的细节。
在三直方图的测试当中,设置值为0.6左右能够较好区分不同场景。
其他测试结果:
【ahash设值20:包容度过高,区分力度不强】
【三直方图设值为0.5左右效果:区分度过于明显,将一些不同景别但画面类似的也过滤了】
- 切割一分钟视频
import cv2
import subprocess
subprocess.call(r'D:/image/ffmpeg/bin/ffmpeg.exe -y -i d:/image/v.mp4 -ss 00:00:0.0 -t 00:01:00.0 -acodec copy -vcodec copy -async 1 d:/image/b3.mp4')
print ('over')