EasyMocap/apps/preprocess/extract_image.py

60 lines
2.3 KiB
Python
Raw Normal View History

2021-12-25 15:26:56 +08:00
'''
@ Date: 2021-08-19 22:06:13
@ Author: Qing Shuai
@ LastEditors: Qing Shuai
2022-08-16 17:59:42 +08:00
@ LastEditTime: 2022-07-12 13:39:25
@ FilePath: /EasyMocapPublic/apps/preprocess/extract_image.py
2021-12-25 15:26:56 +08:00
'''
# extract image from videos
import os
from os.path import join
from glob import glob
extensions = ['.mp4', '.webm', '.flv', '.MP4', '.MOV', '.mov', '.avi']
def run(cmd):
print(cmd)
os.system(cmd)
def extract_images(path, ffmpeg, image):
videos = sorted(sum([
glob(join(path, 'videos', '*'+ext)) for ext in extensions
], [])
)
for videoname in videos:
sub = '.'.join(os.path.basename(videoname).split('.')[:-1])
sub = sub.replace(args.strip, '')
outpath = join(path, image, sub)
2022-08-16 17:59:42 +08:00
if os.path.exists(outpath) and (len(os.listdir(outpath)) > 10 or len(os.listdir(outpath)) == args.num) and not args.restart:
continue
2021-12-25 15:26:56 +08:00
os.makedirs(outpath, exist_ok=True)
other_cmd = ''
if args.num != -1:
other_cmd += '-vframes {}'.format(args.num)
2022-08-16 17:59:42 +08:00
if args.scale != 1 and args.transpose != -1:
other_cmd += ' -vf "transpose={transpose},scale=iw/{scale}:ih/{scale}"'.format(scale=args.scale,transpose=args.transpose)
elif args.scale != 1:
other_cmd += ' -vf "scale=iw/{scale}:ih/{scale}'.format(scale=args.scale)
elif args.transpose != -1:
other_cmd += ' -vf transpose={}'.format(args.transpose)
2021-12-25 15:26:56 +08:00
cmd = '{} -i {} {} -q:v 1 -start_number 0 {}/%06d.jpg'.format(
ffmpeg, videoname, other_cmd, outpath)
2022-08-16 17:59:42 +08:00
if not args.debug:
cmd += ' -loglevel quiet'
2021-12-25 15:26:56 +08:00
run(cmd)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('path', type=str)
parser.add_argument('--strip', type=str, default='')
parser.add_argument('--image', type=str, default='images')
parser.add_argument('--num', type=int, default=-1)
2022-08-16 17:59:42 +08:00
parser.add_argument('--scale', type=int, default=1)
2021-12-25 15:26:56 +08:00
parser.add_argument('--transpose', type=int, default=-1)
parser.add_argument('--ffmpeg', type=str, default='ffmpeg')
2022-08-16 17:59:42 +08:00
parser.add_argument('--restart', action='store_true')
2021-12-25 15:26:56 +08:00
parser.add_argument('--debug', action='store_true')
args = parser.parse_args()
extract_images(args.path, args.ffmpeg, args.image)