Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file type is a media file?

I am trying to loop through a list of files, and return those files that are media files (images, video, gif, audio, etc.).

Seeing as there are a lot of media types, is there a library or perhaps better way to check this, than listing all types then checking a file against that list?

Here's what I'm doing so far:

import os
types = [".mp3", ".mpeg", ".gif", ".jpg", ".jpeg"]
files = ["test.mp3", "test.tmp", "filename.mpg", ".AutoConfig"]

media_files = []
for file in files:
    root, extention = os.path.splitext(file)
    print(extention)
    if extention in types:
        media_files.append(file)

print("Found media files are:")
print(media_files)

But note it didn't include filename.mpg, since I forgot to put .mpg in my types list. (Or, more likely, I didn't expect that list to include a .mpg file, so didn't think to list it out.)

like image 648
BruceWayne Avatar asked Mar 22 '19 21:03

BruceWayne


People also ask

How can I tell what type of file a file is?

Click Start. Open Control Panel, click Control Panel Home, and click Programs. Click Default Programs, and click Associate a file type or protocol with a program. On this screen, the registered file types are displayed.

How can I tell if a file is a video?

get(getArguments(). getString(ARG_ITEM_ID)). get(0)); ` where info_map is a map with (String, ArrayList<String>) pairings where get(0) gives the file name and get(1) right now gets a parameter from the xml for the video type.

How do I know if a file is video or image in Python?

For this purpose you need to get internet media type for file, split it by / character and check if it starts with audio,video,image. NOTE: This method assume the file type by its extension and don't open the actual file, it is based only on the file extension.

How do I find the file type of my media folder?

Here, all the files in your media folders will be listed. Right click on a highlighted file and select Properties (PC) or Get into (Mac). Then, the file type will be listed here. Alternatively, you can identify the file format by checking its extension – the end letters to the right of the mark “. ”.

How do I know what a file is?

With so many different types of files around these days, it’s important that every file you try to open or execute is correctly identified by Windows. This is usually achieved by looking at the file extension of.exe or.jpg for example, and when you double click on one of those files, the system knows what the file is and what to do with it.

How can I see what file types that cannot be played?

However, some players will not show the file names that are in formats that cannot be played. To see what types of files you have in your media library, you need to go to the folder view of Windows Explorer (PC) or Finder (Mac). Here, all the files in your media folders will be listed.

What is a media file and how to open it?

A MEDIA file is a video created by one of several Wi-Fi-enabled security cameras. It contains footage recorded by the camera. MEDIA files are known to be created by Littlelf and LARKKEY cameras, which are sold on Amazon.com. How do I open a MEDIA file? Most MEDIA files can be played only by the software included with the camera that created them.


Video Answer


1 Answers

For this purpose you need to get internet media type for file, split it by / character and check if it starts with audio,video,image.

Here is a sample code:

import mimetypes
mimetypes.init()

mimestart = mimetypes.guess_type("test.mp3")[0]

if mimestart != None:
    mimestart = mimestart.split('/')[0]

    if mimestart in ['audio', 'video', 'image']:
        print("media types")

NOTE: This method assume the file type by its extension and don't open the actual file, it is based only on the file extension.

Creating a module

If you want to create a module that checks if the file is a media file you need to call the init function at the start of the module.

Here is an example of how to create the module:

ismediafile.py

import mimetypes
mimetypes.init()

def isMediaFile(fileName):
    mimestart = mimetypes.guess_type(fileName)[0]

    if mimestart != None:
        mimestart = mimestart.split('/')[0]

        if mimestart in ['audio', 'video', 'image']:
            return True
    
    return False

and there how to use it:

main.py

from ismediafile import isMediaFile

if __name__ == "__main__":
    if isMediaFile("test.mp3"):
        print("Media file")
    else:
        print("not media file")
like image 192
Cpp Forever Avatar answered Sep 23 '22 12:09

Cpp Forever