Reputation: 23
class Metadata(Enum):
bit_rate_kbps: int
sample_rate: int
channel_mode: Literal['mono', 'audio']
file_name: str # includes extension
file_size: int # mb
file_ext: Literal['wav']
@classmethod
def get_metadata_from_bytes(cls, audio: bytes):
command = ['exiftool', '-json', '-']
try:
# Start the exiftool process
process = subprocess.Popen(
command,
stdin=subprocess.PIPE, # Pass data via stdin
stdout=subprocess.PIPE, # Capture stdout
stderr=subprocess.PIPE, # Capture stderr
)
# Send the bytes to exiftool and capture output
stdout, stderr = process.communicate(input=audio)
if process.returncode == 0:
print("Exiftool Output (Bytes):")
print(stdout.decode('utf-8')) # Decode output to text
else:
print("Exiftool Error (Bytes):")
error_message = stderr.decode('utf-8') # Decode errors to text
raise RuntimeError(f"Exiftool failed with error: {error_message}")
except FileNotFoundError:
print("Error: exiftool is not installed or not in the system PATH.")
except RuntimeError as e:
print(f"Subprocess Error: {e}")
except Exception as e:
print(f"Unexpected Error: {e}")
Prints:
[{
"SourceFile": "-",
"ExifToolVersion": 13.10,
"FileModifyDate": "0000:00:00 00:00:00",
"FileAccessDate": "0000:00:00 00:00:00",
"FileCreateDate": "0000:00:00 00:00:00",
"FilePermissions": "p---------",
"FileType": "MP3",
"FileTypeExtension": "mp3",
"MIMEType": "audio/mpeg",
"MPEGAudioVersion": 1,
"AudioLayer": 3,
"AudioBitrate": "64 kbps",
"SampleRate": 48000,
"ChannelMode": "Stereo",
"MSStereo": "Off",
"IntensityStereo": "Off",
"CopyrightFlag": false,
"OriginalMedia": false,
"Emphasis": "None",
"ID3Size": 313,
"EncoderSettings": "Lavf61.7.100"
}]
Running exiftool on cmd on the same file that was used to get the bytes from:
exiftool -r original_sample.mp3 -w .txt original_sample.mp3
Output(original_sample.txt):
ExifTool Version Number : 13.10
File Name : original_sample.mp3
Directory : .
File Size : 25 MB
File Modification Date/Time : 2024:12:28 05:57:46+01:00
File Access Date/Time : 2024:12:31 20:31:54+01:00
File Creation Date/Time : 2024:12:31 19:06:31+01:00
File Permissions : -rw-rw-rw-
File Type : MP3
File Type Extension : mp3
MIME Type : audio/mpeg
MPEG Audio Version : 1
Audio Layer : 3
Audio Bitrate : 64 kbps
Sample Rate : 48000
Channel Mode : Stereo
MS Stereo : Off
Intensity Stereo : Off
Copyright Flag : False
Original Media : False
Emphasis : None
ID3 Size : 313
Encoder Settings : Lavf61.7.100
Duration : 0:51:22 (approx)
How I read the file:
def test_get_metadata_from_bytes():
with open('test/polyvoice/original_sample.mp3', 'rb') as file:
file_bytes = file.read()
metadata = Metadata.get_metadata_from_bytes(file_bytes)
assert True
Could this be because open() gets rid of the file specific metadata such as modifydata, accessdate, etc... ?
I tried running the command in python with -FileName, -FileSize, etc... but it would only output the ones that get output without those flags anyway.
Upvotes: 0
Views: 23