PDA

View Full Version : python/shell recognize that the given file path is audio/video



giuspen
November 2nd, 2009, 02:03 PM
hi,
is there a way through python/shell to recognize a file as audio or video?
actually I check for the file extension into a list

AUDIO_TYPES = ['.mp3','.MP3','.mpa','.MPA','.flac','.FLAC','.ogg' ,'.OGG','.wav','.WAV','.wma','.WMA']
VIDEO_TYPES = ['.avi','.AVI','.wmv','.WMV','.mpg','.MPG','.mpeg', '.MPEG']
AUDIO_VIDEO_TYPES = set(AUDIO_TYPES.extend(VIDEO_TYPES))
if os.path.splitext(source_path)[1] in AUDIO_VIDEO_TYPES:
source_path_list.append(source_path)
but this is not very reliable as the extensions are so many.
thanks in advance.

Arndt
November 2nd, 2009, 02:11 PM
hi,
is there a way through python/shell to recognize a file as audio or video?
actually I check for the file extension into a list

AUDIO_TYPES = ['.mp3','.MP3','.mpa','.MPA','.flac','.FLAC','.ogg' ,'.OGG','.wav','.WAV','.wma','.WMA']
VIDEO_TYPES = ['.avi','.AVI','.wmv','.WMV','.mpg','.MPG','.mpeg', '.MPEG']
AUDIO_VIDEO_TYPES = set(AUDIO_TYPES.extend(VIDEO_TYPES))
if os.path.splitext(source_path)[1] in AUDIO_VIDEO_TYPES:
source_path_list.append(source_path)
but this is not very reliable as the extensions are so many.
thanks in advance.

There is the 'file' command. I don't know if it recognizes all of those. Also, as I just discovered, some of these may consist of pure text, referring to other files. Then 'file' will just report them as text.

giuspen
November 2nd, 2009, 02:19 PM
There is the 'file' command. I don't know if it recognizes all of those. Also, as I just discovered, some of these may consist of pure text, referring to other files. Then 'file' will just report them as text.

that's great thx

Arndt
November 2nd, 2009, 02:22 PM
that's great thx

In python, by the way, you can use the 'magic' module. That may be better than calling the external 'file' program, but I expect they do the same thing.

ghostdog74
November 2nd, 2009, 02:49 PM
the file command also makes use of magic files. check the man page for more.

giuspen
November 2nd, 2009, 04:12 PM
here's a script that works fine:

filetype = subprocess.Popen('file -i "%s"' % source_path, shell=True, stdout=subprocess.PIPE).communicate()[0]
if "audio" in filetype: source_path_list.append(source_path)