-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecksum.py
54 lines (51 loc) · 1.46 KB
/
checksum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from mutagen.flac import FLAC
import os
import subprocess
import locale
def get_md5(input_file, bps):
if bps == 16:
command = [
'ffmpeg',
'-i', input_file,
'-map', '0:a',
'-f', 'md5',
'-'
]
elif bps == 24:
command = [
'ffmpeg',
'-i', input_file,
'-map', '0:a',
'-c:a', 'pcm_s24le',
'-f', 'md5',
'-'
]
else:
raise Exception('只支持16bit/24bit')
try:
p = subprocess.run(command, capture_output=True, check=True)
stdout = str(p.stdout.strip(), locale.getdefaultlocale()[1])
hex_string = stdout.split('=')[1]
if not hex_string:
raise
decimal_value = int(hex_string, 16)
if not decimal_value:
raise
return decimal_value
except subprocess.CalledProcessError as e:
raise Exception(f'FFmpeg Error: {e.stderr.decode()}')
except:
raise
for root, dirs, files in os.walk('.'):
flac_files = [f for f in files if f.endswith('.flac')]
for flac_file in flac_files:
file = os.path.join(root, flac_file)
flac = FLAC(file)
bps = flac.info.bits_per_sample
try:
md5 = get_md5(file, bps)
flac.info.md5_signature = md5
flac.save()
print(f'写入成功:{file}')
except Exception as e:
print(f'错误:{e}')