python 视频图片转换 分辨率处理
2022-04-05 16:49 jym蒟蒻 阅读(408) 评论(0) 编辑 收藏 举报- 单个视频转图片
- 所有图片转视频
- 视频压缩 - ffmpeg
- 图片降低分辨率
- 图像处理
这是最近无聊的想法,对视频进行处理,其实也就是对图片的处理。
对视频进行后期处理,思路就是,视频转图片,然后对图片进行处理,再把图片转视频。
图片处理的多么奇怪,视频就多么奇怪,我当时想了想觉得挺好玩就做了玩玩。
视频可能很大,对视频处理要花好长时间,所以还进行了视频压缩等操作。
用到的代码如下。
'''
提取单个视频的所有帧
'''
import cv2
import numpy as np
def save_image(image,addr,num):
#存储的图片路径
address=addr+str(num)+'.jpg'
#存储图片
cv2.imwrite(address,image)
#读入视频
videoCapture=cv2.VideoCapture("./video/snowman.mp4")
#读取视频帧
success,frame=videoCapture.read()
i=0
while success:
i=i+1
#保存图片
save_image(frame,'./img/',i)
if success:
print('save image:',i)
#读取视频帧
sucess,frame=videoCapture.read()
import cv2
import os
fps = 29#帧速率
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter(filename='./c.mp4', fourcc=fourcc, fps=fps, frameSize=(1080, 1920)) # 图片实际尺寸,不然生成的视频会打不开
for i in range(1,258):
p = i
if os.path.exists('./img/'+str(p)+'.jpg'): #判断图片是否存在
img = cv2.imread(filename='./img/'+str(p)+'.jpg')
#cv2.waitKey(100)
video_writer.write(img)
video_writer.release()
在命令行输入:python ZIP.py ./ c.mp4 c2.mp4
import sys
import os
import zlib
import threading
import platform
from PIL import Image
# python press.py ./ 12.mp4 23.mp4
# python 文件名 路径 要压缩的文件 压缩之后的文件名
# python ZIP.py ./ c.mp4 c2.mp4
#filePath = "./"
#inputName = "c.mp4"
#outName = "c2.mp4"
class Compress_Pic_or_Video(object):
def __init__(self,filePath,inputName,outName=""):
self.filePath = filePath #文件地址
self.inputName = inputName #输入的文件名字
self.outName = outName #输出的文件名字
self.system_ = platform.platform().split("-",1)[0]
if self.system_ == "Windows":
self.filePath = (self.filePath + "\\") if self.filePath.rsplit("\\",1)[-1] else self.filePath
elif self.system_ == "Linux":
self.filePath = (self.filePath + "/") if self.filePath.rsplit("/",1)[-1] else self.filePath
self.fileInputPath = self.filePath + inputName
self.fileOutPath = self.filePath + outName
@property
def is_video(self):
videoSuffixSet = {"WMV","ASF","ASX","RM","RMVB","MP4","3GP","MOV","M4V","AVI","DAT","MKV","FIV","VOB"}
suffix = self.fileInputPath.rsplit(".",1)[-1].upper()
if suffix in videoSuffixSet:
return True
else:
return False
def SaveVideo(self):
fpsize = os.path.getsize(self.fileInputPath) / 1024
if fpsize >= 150.0: #大于150KB的视频需要压缩
if self.outName:
compress = "ffmpeg -i {} -r 10 -pix_fmt yuv420p -vcodec libx264 -preset veryslow -profile:v baseline -crf 23 -acodec aac -b:a 32k -strict -5 {}".format(self.fileInputPath,self.fileOutPath)
isRun = os.system(compress)
else:
compress = "ffmpeg -i {} -r 10 -pix_fmt yuv420p -vcodec libx264 -preset veryslow -profile:v baseline -crf 23 -acodec aac -b:a 32k -strict -5 {}".format(self.fileInputPath, self.fileInputPath)
isRun = os.system(compress)
if isRun != 0:
return (isRun,"没有安装ffmpeg")
return True
else:
return True
def Compress_Video(self):
# 异步保存打开下面的代码,注释同步保存的代码
# thr = threading.Thread(target=self.SaveVideo)
# thr.start()
#下面为同步代码
fpsize