Skip to content

Instantly share code, notes, and snippets.

@taesiri
Created June 28, 2017 19:41
Show Gist options
  • Save taesiri/75fa0f01bb6c9a3360b8560935062931 to your computer and use it in GitHub Desktop.
Save taesiri/75fa0f01bb6c9a3360b8560935062931 to your computer and use it in GitHub Desktop.
Quick and dirty code to extract all frames from mp4 files recusively
from __future__ import print_function
import sys
import os
from glob import glob
import subprocess
ffmpeg_binary = "C:\\Windows\\System32\\ffmpeg.exe"
working_path = sys.argv[1:][0]
mp4_files = [y for x in os.walk(working_path) for y in glob(os.path.join(x[0], '*.mp4'))]
def shoot_ffmpeg(filename, folder):
# print("working on file: " + str(filename))
ffmpeg_input_arg = "-i " + "\"" + filename + "\""
ffmpeg_output_arg = "\"" + os.path.join(folder, "frames-%06d.png") + "\""
exec_command = ffmpeg_binary + " " + ffmpeg_input_arg + " " + ffmpeg_output_arg
print("Executing " + exec_command )
process = subprocess.Popen(exec_command, shell=True, stdout=None)
process.wait()
print(process.returncode)
for mp4_file in mp4_files:
# print (mp4_file)
# print(os.path.dirname(mp4_file))
frame_path = os.path.splitext(mp4_file)[0] + "_frames"
if not os.path.exists(frame_path):
os.makedirs(frame_path)
# print(frame_path)
shoot_ffmpeg(mp4_file, frame_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment