Previously, I wrote about Counting Lines of Code in a given directory. Recently, I had a similar curiousity- “how long are all the video files in this folder?” This seemed to be a good fit for a Python script.
The main question, then, is how do we determine the length of a video file in Python? Once we have that problem solved, we can effectively use the same code as the CodeCount and “sum” all the lengths together in much the same way that the Code Count script sums all the line counts.
The most straightforward approach I discovered was using ffmpeg to retrieve video information, redirecting the output to a temp file, and then finding the listed Duration in that temp file. We then take that determined duration and parse it, and create a Python TimeDelta. We can use that TimeDelta, and sum all the TimeDelta’s of the files in question to determine the total run time of all the videos in the specified folders.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#! /usr/bin/env python import os import sys import re import fnmatch import tempfile import datetime def searchfolder(folder,filemask): filelist=[] for root,subFolders,files in os.walk(folder): for file in files: buildpath=os.path.join(root,file) if os.path.isfile(buildpath): if fnmatch.fnmatch(buildpath,filemask): filelist.append(buildpath) return filelist def getVideoLength(filepath): tmpf = tempfile.NamedTemporaryFile() fname = tmpf.name print(filepath) tmpf.close() sSystem = "D:\\programs\\ffmpeg\\static\\bin\\ffmpeg -i \"%s\" 2> %s" % (filepath, tmpf.name); os.system(sSystem) tmpf = open(fname) lines = tmpf.readlines() tmpf.close() os.remove(tmpf.name) getduration='' for l in lines: l = l.strip() if l.startswith('Duration'): getduration = re.search('Duration: (.*?),', l).group(0).split(':',1)[1].strip(' ,') if getduration!='': try: timeparts = getduration.split(':') #hour,minute,second phour=int(timeparts[0]) pminute=int(timeparts[1]) pseconds=float(timeparts[2]) return datetime.timedelta(hours=phour,minutes=pminute,seconds=pseconds) except: return datetime.timedelta(hours=0,minutes=0,seconds=0) if __name__ == '__main__': searchfor = sys.argv[1] searchin = sys.argv[2] print("examining files matching " + searchfor + " in directory " + searchin) totalcount=datetime.timedelta(seconds=0) for countthese in searchfolder(searchin,searchfor): #print Countlines(countthese) grablength = getVideoLength(countthese) if grablength is not None: totalcount = totalcount + getVideoLength(countthese) print("total length:" + str(totalcount)) |
A bit of checking code to allow for the use of *.* to total all ffmpeg compatible video files rounds this out- the syntax is the same as the Code Counter- VidLength.py <FileMask> <directory< and it will display all the files it analyzes and then a total duration.
Have something to say about this post? Comment!