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.)
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.
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.
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.
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 “. ”.
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.
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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With