Skip to content

Commit 52635c7

Browse files
committed
PICARD-2897: Try to guess format for files with unknown extension
1 parent 3048c58 commit 52635c7

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

picard/formats/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ def open_(filename):
7777
ext = filename[i+1:].lower()
7878
file_format = ext_to_format(ext)
7979
if file_format is None:
80-
return None
81-
audio_file = file_format(filename)
80+
audio_file = guess_format(filename)
81+
else:
82+
audio_file = file_format(filename)
8283
else:
8384
# If there is no extension, try to guess the format based on file headers
8485
audio_file = guess_format(filename)

test/formats/test_util.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Picard, the next-generation MusicBrainz tagger
4+
#
5+
# Copyright (C) 2024 Philipp Wolfer
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License
9+
# as published by the Free Software Foundation; either version 2
10+
# of the License, or (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
21+
import os.path
22+
23+
from test.picardtestcase import PicardTestCase
24+
25+
from picard.formats import util
26+
from picard.formats.id3 import MP3File
27+
28+
29+
class FormatsOpenTest(PicardTestCase):
30+
def setUp(self):
31+
super().setUp()
32+
self.set_config_values({'enabled_plugins': []})
33+
34+
def test_open(self):
35+
file_path = os.path.join('test', 'data', 'test.mp3')
36+
filename = self.copy_file_tmp(file_path, '.mp3')
37+
filetype = util.open_(filename)
38+
self.assertIsInstance(filetype, MP3File)
39+
40+
def test_open_no_extension(self):
41+
file_path = os.path.join('test', 'data', 'test.mp3')
42+
filename = self.copy_file_tmp(file_path, '')
43+
filetype = util.open_(filename)
44+
self.assertIsInstance(filetype, MP3File)
45+
46+
def test_open_unknown_extension(self):
47+
file_path = os.path.join('test', 'data', 'test.mp3')
48+
filename = self.copy_file_tmp(file_path, '.fake')
49+
filetype = util.open_(filename)
50+
self.assertIsInstance(filetype, MP3File)
51+
52+
def test_open_unknown_type(self):
53+
file_path = os.path.join('test', 'data', 'mb.png')
54+
filename = self.copy_file_tmp(file_path, '.ogg')
55+
filetype = util.open_(filename)
56+
self.assertIsNone(filetype)

0 commit comments

Comments
 (0)