1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
import subprocess as sp
import signal
X = 1500
Y = 450
WIDTH = 1920
HEIGHT = 1080
LENGTH = WIDTH*HEIGHT*3
OFFSET = (WIDTH*(Y-1)+X)*3
FFMPEG_BIN = 'ffmpeg'
# 读取视频解压成图片流
in_cmd = [FFMPEG_BIN,
'-i', 'zoom_0.mp4',
'-f', 'image2pipe', # 输出成图片格式
'-pix_fmt', 'rgb24',
'-vcodec','rawvideo', '-']
in_pipe = sp.Popen(in_cmd, stdout=sp.PIPE, stdin=sp.PIPE, bufsize=LENGTH*10)
# 输出到输出流进行压缩
out_cmd = [FFMPEG_BIN,
'-f', 'rawvideo',
'-pix_fmt', 'rgb24',
'-s', '%dx%d' % (WIDTH, HEIGHT), # 由于是图片流,需要显式指定尺寸
'-r', '25',
'-y',
'-an',
'-i', '-',
'output.mp4']
out_pipe = sp.Popen(out_cmd, stdout=sp.PIPE, stdin=sp.PIPE, bufsize=LENGTH*10)
# 处理逻辑
i = 0
prev_raw = None
while True:
raw_image = in_pipe.stdout.read(LENGTH)
if not raw_image:
break
r = raw_image[OFFSET]
if r > 80:
raw_image = prev_raw
out_pipe.stdin.write(raw_image)
in_pipe.stdout.flush()
prev_raw = raw_image
out_pipe.send_signal(signal.SIGINT)
|