janaab commited on
Commit
28d0d8c
·
verified ·
1 Parent(s): 16109e9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import gradio as gr
3
+ import os
4
+
5
+ def get_audio_metadata(file_path):
6
+ try:
7
+ # Run ffmpeg to extract metadata
8
+ result = subprocess.run(
9
+ [
10
+ "ffmpeg",
11
+ "-i",
12
+ file_path,
13
+ "-hide_banner",
14
+ ],
15
+ stderr=subprocess.PIPE,
16
+ text=True
17
+ )
18
+
19
+ # Parse output for metadata
20
+ output = result.stderr
21
+ metadata = {
22
+ "Codec": None,
23
+ "Container": None,
24
+ "Channels": None,
25
+ "Sample Rate": None,
26
+ "Bitrate": None,
27
+ }
28
+
29
+ # Extract codec
30
+ if "Audio:" in output:
31
+ audio_line = [line for line in output.splitlines() if "Audio:" in line]
32
+ if audio_line:
33
+ audio_info = audio_line[0].split()
34
+ metadata["Codec"] = audio_info[audio_info.index("Audio:") + 1]
35
+
36
+ # Extract container format
37
+ if "Input #0," in output:
38
+ container_line = [line for line in output.splitlines() if "Input #0," in line]
39
+ if container_line:
40
+ container_info = container_line[0].split()
41
+ metadata["Container"] = container_info[2].strip(',')
42
+
43
+ # Extract channels, sample rate, and bitrate
44
+ for line in output.splitlines():
45
+ if "Audio:" in line:
46
+ audio_info = line.split()
47
+ for item in audio_info:
48
+ if "Hz" in item:
49
+ metadata["Sample Rate"] = item
50
+ if "kb/s" in item:
51
+ metadata["Bitrate"] = item
52
+ if "stereo" in item or "mono" in item:
53
+ metadata["Channels"] = item
54
+
55
+ return metadata
56
+ except Exception as e:
57
+ return {"Error": str(e)}
58
+
59
+ def process_audio(file):
60
+ # Save the uploaded file temporarily
61
+ temp_file = "temp_audio_file"
62
+ with open(temp_file, "wb") as f:
63
+ f.write(file.read())
64
+
65
+ # Get metadata
66
+ metadata = get_audio_metadata(temp_file)
67
+
68
+ # Remove the temporary file
69
+ os.remove(temp_file)
70
+
71
+ return metadata
72
+
73
+ # Gradio interface
74
+ def create_interface():
75
+ inputs = gr.Audio(label="Upload an audio file", type="file")
76
+ outputs = gr.JSON(label="Audio Metadata")
77
+
78
+ interface = gr.Interface(
79
+ fn=process_audio,
80
+ inputs=inputs,
81
+ outputs=outputs,
82
+ title="Audio Metadata Extractor",
83
+ description="Upload an audio file to extract its codec, container, channels, sample rate, and bitrate using ffmpeg."
84
+ )
85
+ return interface
86
+
87
+ if __name__ == "__main__":
88
+ create_interface().launch()