Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os
import commands
# Paul Solt - get the length of all videos within a folder (recursive)
#
# Depending on the number of files/folders, it can take time to complete
#(100 files ~2.018 seconds to run)
# Set your video folder path here
path = "/Users/Shared/Course Videos/1 - Swift in 31 Days/Swift1 - Handbrake 1080"
def main():
# Grab all the video (.mp4) files recursively for a folder
filenames = []
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".mp4"):
filenames.append(os.path.join(root, file))
#Calculate the total time of all videos in folder
totalTimeInSeconds = 0
for file in filenames:
# Calculate total time
lengthInSeconds = videoLength(file)
totalTimeInSeconds += lengthInSeconds
hours = int(totalTimeInSeconds / 3600)
minutes = int((totalTimeInSeconds - (hours * 3600)) / 60)
print "hours:", hours, "Minutes:", minutes
# Parse the plist format from mdls command on Terminal
# @return the time in seconds
# mdls -plist - filename.mp4
def videoLength(filename):
myCommand = "mdls -plist - "
myCommand = myCommand + "\"" + filename + "\""
status, plistInput = commands.getstatusoutput(myCommand)
videoLength = 0
searchTerm = "<key>kMDItemDurationSeconds</key>"
# 1. find "<key>kMDItemDurationSeconds</key>"
# 2. grab text until next </real>
realStart = "<real>"
realEnd = "</real>"
index = plistInput.find(searchTerm)
# Offset by length of search term
startIndex = index + len(searchTerm)
# Calculate new start/end positions
startIndex = plistInput.find(realStart, startIndex)
endIndex = plistInput.find(realEnd, startIndex)
#offset start by length of realStart text
startIndex += len(realStart)
if startIndex != -1 and endIndex != -1:
#Split and parse as number
numberString = plistInput[startIndex:endIndex]
numberString = numberString.strip()
videoLength = float(numberString)
else:
print
return videoLength
# Run the main method
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.