Skip to content

Instantly share code, notes, and snippets.

@PaulSolt
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulSolt/8188b7f5a90505561a17 to your computer and use it in GitHub Desktop.
Save PaulSolt/8188b7f5a90505561a17 to your computer and use it in GitHub Desktop.
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