From 89740f01dc1e98b93ab8a9ebbd041ad5cbfec1ed Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Fri, 22 May 2020 16:29:51 -0400 Subject: [PATCH] using mimetypes as a backup mime detection method, fixed typo --- src/upscaler.py | 12 ++++++++++-- src/video2x_gui.py | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/upscaler.py b/src/upscaler.py index b69940f..81a795b 100755 --- a/src/upscaler.py +++ b/src/upscaler.py @@ -4,7 +4,7 @@ Name: Video2X Upscaler Author: K4YT3X Date Created: December 10, 2018 -Last Modified: May 17, 2020 +Last Modified: May 22, 2020 Description: This file contains the Upscaler class. Each instance of the Upscaler class is an upscaler on an image or @@ -25,6 +25,7 @@ import copy import gettext import importlib import locale +import mimetypes import pathlib import queue import re @@ -457,6 +458,13 @@ class Upscaler: input_file_type = input_file_mime_type.split('/')[0] input_file_subtype = input_file_mime_type.split('/')[1] + # in case python-magic fails to detect file type + # try guessing file mime type with mimetypes + if input_file_type not in ['image', 'video']: + input_file_mime_type = mimetypes.guess_type(self.current_input_file.name)[0] + input_file_type = input_file_mime_type.split('/')[0] + input_file_subtype = input_file_mime_type.split('/')[1] + # start handling input # if input file is a static image if input_file_type == 'image' and input_file_subtype != 'gif': @@ -530,7 +538,7 @@ class Upscaler: # if file is none of: image, image/gif, video # skip to the next task else: - Avalon.error(_('File {} ({}) neither an image of a video').format(self.current_input_file, input_file_mime_type)) + Avalon.error(_('File {} ({}) neither an image nor a video').format(self.current_input_file, input_file_mime_type)) Avalon.warning(_('Skipping this file')) self.processing_queue.task_done() self.total_processed += 1 diff --git a/src/video2x_gui.py b/src/video2x_gui.py index 24eb945..2fad2d8 100755 --- a/src/video2x_gui.py +++ b/src/video2x_gui.py @@ -4,7 +4,7 @@ Creator: Video2X GUI Author: K4YT3X Date Created: May 5, 2020 -Last Modified: May 17, 2020 +Last Modified: May 22, 2020 """ # local imports @@ -15,6 +15,7 @@ from wrappers.ffmpeg import Ffmpeg # built-in imports import contextlib import json +import mimetypes import os import pathlib import sys @@ -131,6 +132,14 @@ class InputTableModel(QAbstractTableModel): input_file_mime_type = magic.from_file(str(file_path.absolute()), mime=True) input_file_type = input_file_mime_type.split('/')[0] input_file_subtype = input_file_mime_type.split('/')[1] + + # in case python-magic fails to detect file type + # try guessing file mime type with mimetypes + if input_file_type not in ['image', 'video']: + input_file_mime_type = mimetypes.guess_type(file_path.name)[0] + input_file_type = input_file_mime_type.split('/')[0] + input_file_subtype = input_file_mime_type.split('/')[1] + if input_file_type == 'image': if input_file_subtype == 'gif': return 'GIF' @@ -838,6 +847,13 @@ class Video2XMainWindow(QMainWindow): input_file_type = input_file_mime_type.split('/')[0] input_file_subtype = input_file_mime_type.split('/')[1] + # in case python-magic fails to detect file type + # try guessing file mime type with mimetypes + if input_file_type not in ['image', 'video']: + input_file_mime_type = mimetypes.guess_type(input_path.name)[0] + input_file_type = input_file_mime_type.split('/')[0] + input_file_subtype = input_file_mime_type.split('/')[1] + # if input file is an image if input_file_type == 'image':