From cfb7ec81e39663e3167b575d0fd0a16c2fcfec14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20L=C3=B6nnemark?= Date: Sat, 12 Oct 2019 17:38:32 +0200 Subject: [PATCH 1/2] Use subprocess instead of os.system This removes issues caused by file names containing special characters. --- data/create_dataset.py | 6 ++++-- video_to_slomo.py | 26 +++++++++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/data/create_dataset.py b/data/create_dataset.py index 29e7eee..4eb0403 100644 --- a/data/create_dataset.py +++ b/data/create_dataset.py @@ -2,6 +2,7 @@ import os import os.path from shutil import rmtree, move +import subprocess import random # For parsing commandline arguments @@ -37,8 +38,9 @@ def extract_frames(videos, inDir, outDir): for video in videos: os.mkdir(os.path.join(outDir, os.path.splitext(video)[0])) - retn = os.system('{} -i {} -vf scale={}:{} -vsync 0 -qscale:v 2 {}/%04d.jpg'.format(os.path.join(args.ffmpeg_dir, "ffmpeg"), os.path.join(inDir, video), args.img_width, args.img_height, os.path.join(outDir, os.path.splitext(video)[0]))) - if retn: + try: + subprocess.check_call([os.path.join(args.ffmpeg_dir, "ffmpeg"), '-i', os.path.join(inDir, video), '-vf', 'scale={}:{}'.format(args.img_width, args.img_height), '-vsync', '0', '-qscale:v', '2', '{}/%04d.jpg'.format(os.path.join(outDir, os.path.splitext(video)[0]))]) + except subprocess.CalledProcessError: print("Error converting file:{}. Exiting.".format(video)) diff --git a/video_to_slomo.py b/video_to_slomo.py index e9e12ba..1b68f8b 100755 --- a/video_to_slomo.py +++ b/video_to_slomo.py @@ -2,6 +2,7 @@ import argparse import os import os.path +import subprocess import ctypes from shutil import rmtree, move from PIL import Image @@ -66,21 +67,20 @@ def extract_frames(video, outDir): """ - error = "" - print('{} -i {} -vsync 0 -qscale:v 2 {}/%06d.jpg'.format(os.path.join(args.ffmpeg_dir, "ffmpeg"), video, outDir)) - retn = os.system('{} -i {} -vsync 0 -qscale:v 2 {}/%06d.jpg'.format(os.path.join(args.ffmpeg_dir, "ffmpeg"), video, outDir)) - if retn: - error = "Error converting file:{}. Exiting.".format(video) - return error + cmd = [os.path.join(args.ffmpeg_dir, "ffmpeg"), '-i', video, '-vsync', '0', '-qscale:v', '2', '{}/%06d.jpg'.format(outDir)] + print(' '.join(cmd)) + try: + subprocess.check_call(cmd) + except CalledProcessError: + return "Error converting file:{}. Exiting.".format(video) def create_video(dir): - error = "" - print('{} -r {} -i {}/%d.jpg -qscale:v 2 {}'.format(os.path.join(args.ffmpeg_dir, "ffmpeg"), args.fps, dir, args.output)) - retn = os.system('{} -r {} -i {}/%d.jpg -crf 17 -vcodec libx264 {}'.format(os.path.join(args.ffmpeg_dir, "ffmpeg"), args.fps, dir, args.output)) - if retn: - error = "Error creating output video. Exiting." - return error - + cmd = [os.path.join(args.ffmpeg_dir, "ffmpeg"), '-r', str(args.fps), '-i', '{}/%d.jpg'.format(dir), '-crf', '17', '-vcodec', 'libx264', args.output] + print(' '.join(cmd)) + try: + subprocess.check_call(cmd) + except CalledProcessError: + return "Error creating output video. Exiting." def main(): # Check if arguments are okay From 0b4d246cdd4fc966e9141375b733867724d6f598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20L=C3=B6nnemark?= Date: Sat, 12 Oct 2019 17:47:46 +0200 Subject: [PATCH 2/2] create_dataset: Increase frame file name lengths The previous length of 4 digits would cause frames out of order if you used a video with more than 9999 frames, e.g. a 240 fps longer than ~42 seconds. The new length of 9 should be high enough, with good margin (allowing for 48+ days of 240 fps video). --- data/create_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/create_dataset.py b/data/create_dataset.py index 4eb0403..ba03650 100644 --- a/data/create_dataset.py +++ b/data/create_dataset.py @@ -39,7 +39,7 @@ def extract_frames(videos, inDir, outDir): for video in videos: os.mkdir(os.path.join(outDir, os.path.splitext(video)[0])) try: - subprocess.check_call([os.path.join(args.ffmpeg_dir, "ffmpeg"), '-i', os.path.join(inDir, video), '-vf', 'scale={}:{}'.format(args.img_width, args.img_height), '-vsync', '0', '-qscale:v', '2', '{}/%04d.jpg'.format(os.path.join(outDir, os.path.splitext(video)[0]))]) + subprocess.check_call([os.path.join(args.ffmpeg_dir, "ffmpeg"), '-i', os.path.join(inDir, video), '-vf', 'scale={}:{}'.format(args.img_width, args.img_height), '-vsync', '0', '-qscale:v', '2', '{}/%09d.jpg'.format(os.path.join(outDir, os.path.splitext(video)[0]))]) except subprocess.CalledProcessError: print("Error converting file:{}. Exiting.".format(video))