Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PICARD-1685 : Add ~filesize variable #2361

Merged
merged 10 commits into from
Jan 18, 2024
7 changes: 6 additions & 1 deletion picard/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
normpath,
thread,
tracknum_and_title_from_filename,
encode_filename,
)
from picard.util.filenaming import (
get_available_filename,
Expand Down Expand Up @@ -132,7 +133,7 @@ class File(QtCore.QObject, Item):

EXTENSIONS = []

FILE_INFO_TAGS = ('~bitrate', '~sample_rate', '~channels', '~bits_per_sample', '~format')
FILE_INFO_TAGS = ('~bitrate', '~sample_rate', '~channels', '~bits_per_sample', '~format', '~filesize')

comparison_weights = {
'title': 13,
Expand Down Expand Up @@ -760,6 +761,10 @@ def can_view_info(self):
return True

def _info(self, metadata, file):
try:
metadata['~filesize'] = os.path.getsize(encode_filename(self.filename))
phw marked this conversation as resolved.
Show resolved Hide resolved
except BaseException:
nullHawk marked this conversation as resolved.
Show resolved Hide resolved
pass
if hasattr(file.info, 'length'):
metadata.length = int(file.info.length * 1000)
if hasattr(file.info, 'bitrate') and file.info.bitrate:
Expand Down
3 changes: 3 additions & 0 deletions picard/formats/wav.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


from collections.abc import MutableMapping
import os

import mutagen

Expand All @@ -33,6 +34,7 @@
from picard.file import File
from picard.formats.id3 import NonCompatID3File
from picard.metadata import Metadata
from picard.util import encode_filename


try:
Expand Down Expand Up @@ -235,6 +237,7 @@ def _load(self, filename):
metadata['~sample_rate'] = f.getframerate()
metadata.length = 1000 * f.getnframes() // f.getframerate()
metadata['~format'] = self.NAME
metadata['~filesize'] = os.path.getsize(encode_filename(self.filename))
self._add_path_to_metadata(metadata)
return metadata

Expand Down
7 changes: 2 additions & 5 deletions picard/ui/infodialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
namedtuple,
)
from html import escape
import os.path
import re
import traceback

Expand Down Expand Up @@ -286,12 +285,10 @@ def format_file_info(file_):
info.append((_("Filename:"), file_.filename))
if '~format' in file_.orig_metadata:
info.append((_("Format:"), file_.orig_metadata['~format']))
try:
size = os.path.getsize(encode_filename(file_.filename))
if '~filesize' in file_.orig_metadata:
size = file_.orig_metadata['~filesize']
nullHawk marked this conversation as resolved.
Show resolved Hide resolved
nullHawk marked this conversation as resolved.
Show resolved Hide resolved
sizestr = "%s (%s)" % (bytes2human.decimal(size), bytes2human.binary(size))
info.append((_("Size:"), sizestr))
except BaseException:
pass
if file_.orig_metadata.length:
info.append((_("Length:"), format_time(file_.orig_metadata.length)))
if '~bitrate' in file_.orig_metadata:
Expand Down
1 change: 1 addition & 0 deletions picard/ui/itemviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class MainPanel(QtWidgets.QSplitter):
(N_("Catalog No."), 'catalognumber'),
(N_("Barcode"), 'barcode'),
(N_("Media"), 'media'),
(N_("Size"), '~filesize'),
(N_("Genre"), 'genre'),
(N_("Fingerprint status"), '~fingerprint'),
(N_("Date"), 'date'),
Expand Down
2 changes: 2 additions & 0 deletions test/formats/test_aac.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AACTest(CommonTests.SimpleFormatsTestCase):
'~channels': '2',
'~sample_rate': '44100',
'~bitrate': '123.824',
'~filesize': '1896',
}
unexpected_info = ['~video']

Expand All @@ -53,6 +54,7 @@ class AACWithAPETest(CommonApeTests.ApeTestCase):
'~channels': '2',
'~sample_rate': '44100',
'~bitrate': '123.824',
'~filesize': '1974',
}
unexpected_info = ['~video']

Expand Down
2 changes: 2 additions & 0 deletions test/formats/test_ac3.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class AC3WithAPETest(CommonApeTests.ApeTestCase):
'~bitrate': '192.0',
'~sample_rate': '44100',
'~channels': '2',
'~filesize': '2570',
}
unexpected_info = ['~video']

Expand Down Expand Up @@ -109,6 +110,7 @@ class EAC3Test(CommonTests.SimpleFormatsTestCase):
'length': 107,
'~sample_rate': '44100',
'~channels': '2',
'~filesize': '2570',
}
unexpected_info = ['~video']

Expand Down
9 changes: 8 additions & 1 deletion test/formats/test_apev2.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class MonkeysAudioTest(CommonApeTests.ApeTestCase):
'~channels': '2',
'~sample_rate': '44100',
'~bits_per_sample': '16',
'~filesize': '2496',
}
unexpected_info = ['~video']

Expand All @@ -183,6 +184,7 @@ class WavPackTest(CommonApeTests.ApeTestCase):
'length': 82,
'~channels': '2',
'~sample_rate': '44100',
'~filesize': '2542',
}
unexpected_info = ['~video']

Expand Down Expand Up @@ -247,6 +249,7 @@ class MusepackSV7Test(CommonApeTests.ApeTestCase):
'length': 91,
'~channels': '2',
'~sample_rate': '44100',
'~filesize': '1605',
}
unexpected_info = ['~video']

Expand All @@ -258,6 +261,7 @@ class MusepackSV8Test(CommonApeTests.ApeTestCase):
'length': 82,
'~channels': '2',
'~sample_rate': '44100',
'~filesize': '1633',
}
unexpected_info = ['~video']

Expand All @@ -274,7 +278,8 @@ def setUp(self):
'length': 82,
'~channels': '2',
'~sample_rate': '44100',
'~bits_per_sample': '16'
'~bits_per_sample': '16',
'~filesize': '2144',
}


Expand All @@ -285,6 +290,7 @@ class OptimFROGLosslessTest(CommonApeTests.ApeTestCase):
'length': 0,
'~channels': '2',
'~sample_rate': '48000',
'~filesize': '181',
}
unexpected_info = ['~video']

Expand All @@ -300,6 +306,7 @@ class OptimFROGDUalStreamTest(CommonApeTests.ApeTestCase):
'length': 0,
'~channels': '2',
'~sample_rate': '48000',
'~filesize': '181',
}
unexpected_info = ['~video']

Expand Down
3 changes: 3 additions & 0 deletions test/formats/test_asf.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class ASFTest(CommonAsfTests.AsfTestCase):
'~channels': '2',
'~sample_rate': '44100',
'~bitrate': '128.0',
'~filesize': '4881',
}


Expand All @@ -116,6 +117,7 @@ class WMATest(CommonAsfTests.AsfTestCase):
'~channels': '2',
'~sample_rate': '44100',
'~bitrate': '64.0',
'~filesize': '8164',
}
unexpected_info = ['~video']

Expand All @@ -129,6 +131,7 @@ class WMVTest(CommonAsfTests.AsfTestCase):
'~sample_rate': '44100',
'~bitrate': '128.0',
'~video': '1',
'~filesize': '8453',
}


Expand Down
6 changes: 6 additions & 0 deletions test/formats/test_id3.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ class MP3Test(CommonId3Tests.Id3TestCase):
'length': 156,
'~channels': '2',
'~sample_rate': '44100',
'~filesize': '2888',
}
unexpected_info = ['~video']

Expand Down Expand Up @@ -641,6 +642,7 @@ class TTATest(CommonId3Tests.Id3TestCase):
expected_info = {
'length': 82,
'~sample_rate': '44100',
'~filesize': '3464',
}
unexpected_info = ['~video']

Expand All @@ -654,6 +656,7 @@ class DSFTest(CommonId3Tests.Id3TestCase):
'~sample_rate': '5644800',
'~bitrate': '11289.6',
'~bits_per_sample': '1',
'~filesize': '114022',
}
unexpected_info = ['~video']

Expand All @@ -668,6 +671,7 @@ class DSDIFFTest(CommonId3Tests.Id3TestCase):
'~sample_rate': '5644800',
'~bitrate': '11289.6',
'~bits_per_sample': '1',
'~filesize': '15288',
}
unexpected_info = ['~video']

Expand All @@ -679,6 +683,7 @@ class DSDIFFDSTTest(CommonId3Tests.Id3TestCase):
'~channels': '2',
'~sample_rate': '5644800',
'~bits_per_sample': '1',
'~filesize': '2214',
}
unexpected_info = ['~video']

Expand All @@ -691,6 +696,7 @@ class AIFFTest(CommonId3Tests.Id3TestCase):
'~channels': '2',
'~sample_rate': '44100',
'~bitrate': '1411.2',
'~filesize': '15704',
}
unexpected_info = ['~video']

Expand Down
3 changes: 2 additions & 1 deletion test/formats/test_midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class MIDITest(CommonTests.SimpleFormatsTestCase):
testfile = 'test.mid'
expected_info = {
'length': 127997,
'~format': 'Standard MIDI File'
'~format': 'Standard MIDI File',
'~filesize': '8444',
}
unexpected_info = ['~video']

Expand Down
2 changes: 2 additions & 0 deletions test/formats/test_mp4.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class M4ATest(CommonMP4Tests.MP4TestCase):
'~sample_rate': '44100',
'~bitrate': '14.376',
'~bits_per_sample': '16',
'~filesize': '3591',
}
unexpected_info = ['~video']

Expand All @@ -168,6 +169,7 @@ class M4VTest(CommonMP4Tests.MP4TestCase):
'~bitrate': '108.043',
'~bits_per_sample': '16',
'~video': '1',
'~filesize': '5097',
}


Expand Down
6 changes: 6 additions & 0 deletions test/formats/test_vorbis.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class FLACTest(CommonVorbisTests.VorbisTestCase):
'~channels': '2',
'~sample_rate': '44100',
'~format': 'FLAC',
'~filesize': '6546',
}
unexpected_info = ['~video']

Expand Down Expand Up @@ -301,6 +302,7 @@ class OggVorbisTest(CommonVorbisTests.VorbisTestCase):
'length': 82,
'~channels': '2',
'~sample_rate': '44100',
'~filesize': '5221',
}


Expand All @@ -311,6 +313,7 @@ class OggSpxTest(CommonVorbisTests.VorbisTestCase):
'length': 89,
'~channels': '2',
'~bitrate': '29.6',
'~filesize': '608',
}
unexpected_info = ['~video']

Expand All @@ -321,6 +324,7 @@ class OggOpusTest(CommonVorbisTests.VorbisTestCase):
expected_info = {
'length': 82,
'~channels': '2',
'~filesize': '1637',
}
unexpected_info = ['~video']

Expand All @@ -340,6 +344,7 @@ class OggTheoraTest(CommonVorbisTests.VorbisTestCase):
'length': 520,
'~bitrate': '200.0',
'~video': '1',
'~filesize': '5298',
}


Expand All @@ -349,6 +354,7 @@ class OggFlacTest(CommonVorbisTests.VorbisTestCase):
expected_info = {
'length': 82,
'~channels': '2',
'~filesize': '2573',
}
unexpected_info = ['~video']

Expand Down