Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this file a video (Python)?

I am trying to create a program in Python 2.7 which has the following requirements:

  1. Enumerates all video files in Windows.
  2. Opens each video file using: os.open(filepath, os.O_RDWR)
  3. Processes the file.

My current algorithm is:

  1. Having a list of all video file extensions. I got them from here
  2. Loop through all files
  3. If the file has a video extension, then process it

This method has following disadvantages:

  1. Some file extensions like '.dat' or '.am' are used for non media files.
  2. Some files with extensions like '.dat' are used by other programs (i.e Antivirus)

What should I do to make a program which satisfies the requirements pointed above?

Here is my current code:

def is_video_file(filename):
    video_file_extensions = (
'.264', '.3g2', '.3gp', '.3gp2', '.3gpp', '.3gpp2', '.3mm', '.3p2', '.60d', '.787', '.89', '.aaf', '.aec', '.aep', '.aepx',
'.aet', '.aetx', '.ajp', '.ale', '.am', '.amc', '.amv', '.amx', '.anim', '.aqt', '.arcut', '.arf', '.asf', '.asx', '.avb',
'.avc', '.avd', '.avi', '.avp', '.avs', '.avs', '.avv', '.axm', '.bdm', '.bdmv', '.bdt2', '.bdt3', '.bik', '.bin', '.bix',
'.bmk', '.bnp', '.box', '.bs4', '.bsf', '.bvr', '.byu', '.camproj', '.camrec', '.camv', '.ced', '.cel', '.cine', '.cip',
'.clpi', '.cmmp', '.cmmtpl', '.cmproj', '.cmrec', '.cpi', '.cst', '.cvc', '.cx3', '.d2v', '.d3v', '.dat', '.dav', '.dce',
'.dck', '.dcr', '.dcr', '.ddat', '.dif', '.dir', '.divx', '.dlx', '.dmb', '.dmsd', '.dmsd3d', '.dmsm', '.dmsm3d', '.dmss',
'.dmx', '.dnc', '.dpa', '.dpg', '.dream', '.dsy', '.dv', '.dv-avi', '.dv4', '.dvdmedia', '.dvr', '.dvr-ms', '.dvx', '.dxr',
'.dzm', '.dzp', '.dzt', '.edl', '.evo', '.eye', '.ezt', '.f4p', '.f4v', '.fbr', '.fbr', '.fbz', '.fcp', '.fcproject',
'.ffd', '.flc', '.flh', '.fli', '.flv', '.flx', '.gfp', '.gl', '.gom', '.grasp', '.gts', '.gvi', '.gvp', '.h264', '.hdmov',
'.hkm', '.ifo', '.imovieproj', '.imovieproject', '.ircp', '.irf', '.ism', '.ismc', '.ismv', '.iva', '.ivf', '.ivr', '.ivs',
'.izz', '.izzy', '.jss', '.jts', '.jtv', '.k3g', '.kmv', '.ktn', '.lrec', '.lsf', '.lsx', '.m15', '.m1pg', '.m1v', '.m21',
'.m21', '.m2a', '.m2p', '.m2t', '.m2ts', '.m2v', '.m4e', '.m4u', '.m4v', '.m75', '.mani', '.meta', '.mgv', '.mj2', '.mjp',
'.mjpg', '.mk3d', '.mkv', '.mmv', '.mnv', '.mob', '.mod', '.modd', '.moff', '.moi', '.moov', '.mov', '.movie', '.mp21',
'.mp21', '.mp2v', '.mp4', '.mp4v', '.mpe', '.mpeg', '.mpeg1', '.mpeg4', '.mpf', '.mpg', '.mpg2', '.mpgindex', '.mpl',
'.mpl', '.mpls', '.mpsub', '.mpv', '.mpv2', '.mqv', '.msdvd', '.mse', '.msh', '.mswmm', '.mts', '.mtv', '.mvb', '.mvc',
'.mvd', '.mve', '.mvex', '.mvp', '.mvp', '.mvy', '.mxf', '.mxv', '.mys', '.ncor', '.nsv', '.nut', '.nuv', '.nvc', '.ogm',
'.ogv', '.ogx', '.osp', '.otrkey', '.pac', '.par', '.pds', '.pgi', '.photoshow', '.piv', '.pjs', '.playlist', '.plproj',
'.pmf', '.pmv', '.pns', '.ppj', '.prel', '.pro', '.prproj', '.prtl', '.psb', '.psh', '.pssd', '.pva', '.pvr', '.pxv',
'.qt', '.qtch', '.qtindex', '.qtl', '.qtm', '.qtz', '.r3d', '.rcd', '.rcproject', '.rdb', '.rec', '.rm', '.rmd', '.rmd',
'.rmp', '.rms', '.rmv', '.rmvb', '.roq', '.rp', '.rsx', '.rts', '.rts', '.rum', '.rv', '.rvid', '.rvl', '.sbk', '.sbt',
'.scc', '.scm', '.scm', '.scn', '.screenflow', '.sec', '.sedprj', '.seq', '.sfd', '.sfvidcap', '.siv', '.smi', '.smi',
'.smil', '.smk', '.sml', '.smv', '.spl', '.sqz', '.srt', '.ssf', '.ssm', '.stl', '.str', '.stx', '.svi', '.swf', '.swi',
'.swt', '.tda3mt', '.tdx', '.thp', '.tivo', '.tix', '.tod', '.tp', '.tp0', '.tpd', '.tpr', '.trp', '.ts', '.tsp', '.ttxt',
'.tvs', '.usf', '.usm', '.vc1', '.vcpf', '.vcr', '.vcv', '.vdo', '.vdr', '.vdx', '.veg','.vem', '.vep', '.vf', '.vft',
'.vfw', '.vfz', '.vgz', '.vid', '.video', '.viewlet', '.viv', '.vivo', '.vlab', '.vob', '.vp3', '.vp6', '.vp7', '.vpj',
'.vro', '.vs4', '.vse', '.vsp', '.w32', '.wcp', '.webm', '.wlmp', '.wm', '.wmd', '.wmmp', '.wmv', '.wmx', '.wot', '.wp3',
'.wpl', '.wtv', '.wve', '.wvx', '.xej', '.xel', '.xesc', '.xfl', '.xlmv', '.xmv', '.xvid', '.y4m', '.yog', '.yuv', '.zeg',
'.zm1', '.zm2', '.zm3', '.zmv'  )

if filename.endswith((video_file_extensions)):
    return True

def get_drives():
    import string
    from ctypes import windll
    import ctypes
    from ctypes.wintypes import MAX_PATH

    drives = []
    bitmask = windll.kernel32.GetLogicalDrives()
    for letter in string.uppercase:
        if bitmask & 1:
            letter = letter + ':\\'
            drives.append(letter)
        bitmask >>= 1
    return drives

def process(filepath):
    try:
        video_file = os.open(filepath, os.O_RDWR)
        # Do processing



def process_all_videos():
    for dirve in drives:
        for root, dirs, files in os.walk(dirve):
            for filename in files:
                if is_video_file(filename):
                    try:
                        filepath = os.path.join(root, filename)
                        process(filepath)


def main():
    process_all_videos()

if __name__ == '__main__':
    main()
like image 214
Ababneh A Avatar asked Feb 17 '13 09:02

Ababneh A


4 Answers

Try using python-magic, which is a better detector of the file's type by reading its contents (usually the first 1024 bytes).

like image 89
Burhan Khalid Avatar answered Sep 28 '22 07:09

Burhan Khalid


At this point in time, there's an even easier way to check if any given file contains a valid video stream: load up the file using the MediaInfo wrapper for Python and check for the existence of a video stream.

fileInfo = MediaInfo.parse('some/file/name.ext')
for track in fileInfo.tracks:
    if track.track_type == "Video":
        # success!

I don't know about the efficiency of this, but it should be at least as fast as doing it with ffmpeg. At the very least, it has a nice API, and there's a lot of other great info that you can easily extract with it.

like image 31
Dakota Avatar answered Sep 28 '22 05:09

Dakota


This is the effective solution for your problem

import mimetypes

if mimetypes.guess_type('file_path')[0].startswith('video'):
   print('It is a video')```
like image 37
Prajwal K Gowda Avatar answered Sep 28 '22 06:09

Prajwal K Gowda


I was struggling with the same issue, this was my solution:

import magic
mime = magic.Magic(mime=True)
filename = mime.from_file(path_to_video)
if filename.find('video') != -1:
   print('it is video') 
like image 20
Yustina Ivanova Avatar answered Sep 28 '22 05:09

Yustina Ivanova