HussainLatiff Banuka commited on
Commit
3758a52
Β·
verified Β·
1 Parent(s): 2f9c108

Added the blobStorage.py file (#7)

Browse files

- Added the blobStorage.py file (2fe1d199c6781f70d87184c00be4cb89af32f9af)


Co-authored-by: Nanayakkara <Banuka@users.noreply.huggingface.co>

Files changed (1) hide show
  1. blobStorage.py +50 -0
blobStorage.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from azure.storage.blob import BlobServiceClient
2
+ from io import BytesIO
3
+ from pydub import AudioSegment
4
+ import os
5
+ import ffmpeg
6
+
7
+ audio_clip_mapping = {
8
+ "key1": "3_second_explosion_00001.flac",
9
+ # Add more key-value pairs as needed
10
+ }
11
+
12
+ # Initialize Azure Blob Storage client
13
+ account_name = 'phonebrrdemonstration2'
14
+ account_key = 'Q+EneUx5hlODHCjsSo49mm1bGVNdBd2wZ/T0yZMtag1C6FUIwr/yKf+XqDPmVF1PU81eitB2L3tN+AStD/eZ+A=='
15
+
16
+ blob_service_client = BlobServiceClient(
17
+ account_url=f"https://{account_name}.blob.core.windows.net",
18
+ credential=account_key
19
+ )
20
+
21
+ # Define the container name
22
+ container_name = 'audio3second0001' # should define the what container should access
23
+
24
+ # Define a function to retrieve audio clips by key
25
+ def retrieve_audio_clip(key):
26
+ # Get the blob name corresponding to the key
27
+ blob_name = audio_clip_mapping.get(key)
28
+ if blob_name is None:
29
+ print(f"No audio clip found for key: {key}")
30
+ return None
31
+
32
+ # Get the blob client for the audio clip
33
+ blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
34
+
35
+ # Stream the audio data directly into an AudioSegment object
36
+ stream = BytesIO()
37
+ blob_client.download_blob().download_to_stream(stream)
38
+ stream.seek(0)
39
+
40
+ audio_segment = AudioSegment.from_file(stream)
41
+ stream.close() # Close the stream to avoid memory leaks
42
+ return audio_segment
43
+
44
+ # Example usage:
45
+ key_to_retrieve = "key1"
46
+ retrieved_audio_clip = retrieve_audio_clip(key_to_retrieve)
47
+
48
+ if retrieved_audio_clip:
49
+ print(f"Retrieved audio clip for key {key_to_retrieve}")
50
+ # Further process retrieved_audio_clip as needed