-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcut_video.py
More file actions
89 lines (77 loc) · 2.74 KB
/
cut_video.py
File metadata and controls
89 lines (77 loc) · 2.74 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -*- coding: utf-8 -*-
# @Time : 2022/4/12 13:55
# @Author : beyoung
# @Email : linbeyoung@stu.pku.edu.cn
# @File : cut_video.py
import os
import cv2
import argparse
import tqdm
def one_cut(ip_path, filename, op_path):
name = filename[:-4]
video_pth = os.path.join(ip_path, filename)
print(video_pth)
vc = cv2.VideoCapture(video_pth) # 读取视频文件,修改为自己的文件名
c = 0
print("------------")
if vc.isOpened():
# print("yes")
rval, frame = vc.read()
else:
rval = False
print("false")
timeF = 60 # 视频帧计数间隔
if not os.path.exists(op_path):
os.makedirs(op_path)
while rval: # 循环读取视频
rval, frame = vc.read()
# print(c, timeF, c % timeF)
if (c % timeF == 0):
# print("write...")
# cv2.imwrite(f"{op_path}photo_{}.jpg".format(c), frame) # 修改为自己的文件夹
cv2.imwrite(os.path.join(op_path, f"{name}_{c}.png"), frame) # 修改为自己的文件夹
# print("success!")
c = c + 1
cv2.waitKey(1)
vc.release()
print("==================================")
def batch_cut(ip_path, op_path, resize_ratio=0.2):
"""
本函数可以将ip_path下所有tif结尾图片的长宽缩成原本的0.2, 并保存在op_path中
:param ip_path:
:param op_path:
:param resize_ratio:ip_path, filename, op_path)
:return:
"""
#批量缩放
# resize_ratio = 0.2
if not os.path.exists(op_path):
os.makedirs(op_path)
files = os.listdir(ip_path)
files.sort()
for filename in tqdm.tqdm(files):
if filename.endswith('_带弹幕.mp4'):
# if filename.endswith('14-14 Patrol Randomly 随机巡逻点-1080P 高清-AVC.mp4'):
try:
one_cut(ip_path, filename, op_path)
except:
print(filename)
def batch_rename(path):
# 批量重命名
for filename in os.listdir(path):
im = cv2.imread(filename)
name = filename.split('.')
# print(name[1])
if name[1] == "tif":
os.rename(os.path.join(path, filename), os.path.join(path, name[0] + '.tif'))
if __name__ == '__main__':
# Parse arguments
# parser = argparse.ArgumentParser()
# parser.add_argument("--ip_path", type=str, help="原路径")
# parser.add_argument("--op_path", type=str, help="压缩后输出路径")
# parser.add_argument("--resize_ratio", type= int, default=0.2, help="压缩比例")
# args = parser.parse_args()
# args.ip_path = 'Unity2018教程2D入门_final/combine_video'
# args.op_path = 'Unity2018教程2D入门_带弹幕_screenshot_9_30_60'
# batch_cut(args.ip_path, args.op_path, args.resize_ratio)
pass