import subprocess import gradio as gr import os def get_audio_metadata(file_path): try: # Run ffmpeg to extract metadata result = subprocess.run( [ "ffmpeg", "-i", file_path, "-hide_banner", ], stderr=subprocess.PIPE, text=True ) # Parse output for metadata output = result.stderr metadata = { "Container": None, "Codec": None, "Channels": None, "Sample Format": None, "Sample Rate": None, "Bitrate": None, } # Extract container format if "Input #0," in output: container_line = [line for line in output.splitlines() if "Input #0," in line] if container_line: container_info = container_line[0].split() metadata["Container"] = container_info[2].strip(',') # Extract codec, channels, sample format, sample rate, and bitrate if "Audio:" in output: audio_line = [line for line in output.splitlines() if "Audio:" in line] if audio_line: audio_info = audio_line[0].split() metadata["Codec"] = audio_info[audio_info.index("Audio:") + 1] audio_info = audio_line[0].split(",") for item in audio_info: if "Hz" in item: metadata["Sample Rate"] = item.strip() if "kb/s" in item: metadata["Bitrate"] = item.strip() if "stereo" in item or "mono" in item: metadata["Channels"] = item.strip() if "s32" in item or "s16" in item or "flt" in item: metadata["Sample Format"] = item.strip() return metadata except Exception as e: return {"Error": str(e)} def process_audio(file): # Get metadata metadata = get_audio_metadata(file) return metadata # Gradio interface def create_interface(): inputs = gr.Audio(label="Upload an audio file", type="filepath") outputs = gr.JSON(label="Audio Metadata") interface = gr.Interface( fn=process_audio, inputs=inputs, outputs=outputs, title="FFmpeg.info", description="Upload an audio file to extract its container, codec, channels, sample format, sample rate, and bitrate using ffmpeg." ) return interface if __name__ == "__main__": create_interface().launch()