-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaudio.py
More file actions
35 lines (24 loc) · 917 Bytes
/
audio.py
File metadata and controls
35 lines (24 loc) · 917 Bytes
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
"""Manage audio files independently of the media player."""
import filetype
def get_audio_files(filepath):
"""Get audio files with the same name as provided file.
Args:
filepath (pathlib.Path): Path of the initial file.
Returns:
list of pathlib.Path: List of paths of audio files.
"""
# list files with similar stem
items = filepath.parent.glob(f"{filepath.stem}.*")
return [item for item in items if item != filepath and is_audio_file(item)]
def is_audio_file(file_path):
"""Detect if a file is audio file based on standard magic numbers.
Args:
file_path (pathlib.Path): Path of the file to investigate.
Returns:
bool: `True` if the file is an audio file, `False` otherwise.
"""
kind = filetype.guess(str(file_path))
if not kind:
return False
maintype, _ = kind.mime.split("/")
return maintype == "audio"