python 视频滤镜_Python 视频处理:滤镜之赛博朋克

本文介绍如何使用Python将夜景视频转换为赛博朋克风格。通过结合图片滤镜处理技术和ffmpeg库,实现了视频帧的逐帧处理,创建出渐变式的局部滤镜效果。
摘要由CSDN通过智能技术生成

前言

处理视频滤镜之前,需要先掌握图片滤镜的处理,如何将一张夜景图片修改为赛博朋克的风格?

视频滤镜

夜景素材

渲染效果

Python 代码

import ffmpeg

import numpy as np

import os

from image.cyber import cyberpunk

import cv2

if __name__ == '__main__':

# 源视频

video_path = 'night.mp4'

video_probe = ffmpeg.probe(video_path)

video_info = next((stream for stream in video_probe['streams'] if stream['codec_type'] == 'video'), None)

video_frames = int(video_info['nb_frames'])

width = int(video_info['width'])

height = int(video_info['height'])

video_input = ffmpeg.input(video_path)

in_process = (

video_input.video.output('pipe:', format='rawvideo', pix_fmt='rgb24', r=30).run_async(pipe_stdout=True)

)

# 滤镜视频流

tmp_path = 'night_tmp.mp4'

tmp_process = (

ffmpeg

.input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width, height), framerate=30)

.output(tmp_path, pix_fmt='yuv420p', r=30)

.overwrite_output()

.run_async(pipe_stdin=True)

)

frame_index = 1

# 视频帧处理

while True:

in_bytes = in_process.stdout.read(width * height * 3)

if not in_bytes:

break

in_frame = (

np

.frombuffer(in_bytes, np.uint8)

.reshape([height, width, 3])

)

# 渐变式局部滤镜视频,过渡时间 5 秒,帧率为 30,则此处设置的值为 150

in_frame_bgr = cv2.cvtColor(in_frame, cv2.COLOR_RGB2BGR)

current_width = int(width * (frame_index / 150))

in_frame_bgr[:, 0:current_width, :] = cyberpunk(in_frame_bgr[:, 0:current_width, :])

in_frame = cv2.cvtColor(in_frame_bgr, cv2.COLOR_BGR2RGB)

tmp_process.stdin.write(

in_frame

.astype(np.uint8)

.tobytes()

)

if frame_index < 150:

frame_index += 1

# 等待异步处理完毕

tmp_process.stdin.close()

in_process.wait()

tmp_process.wait()

# 将原始视频的音乐合并到新视频

result_path = 'night_new.mp4'

(

ffmpeg.input(tmp_path)

.output(video_input.audio, result_path, r=30)

.run(overwrite_output=True)

)

# 删除临时文件

os.remove(tmp_path)

素材下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值