File size: 8,232 Bytes
b666de2
9dad242
dee3e42
8559a47
dee3e42
8559a47
 
1fbe333
790b170
 
3cb44aa
b26607d
 
 
 
 
 
 
 
 
 
b666de2
29b136c
dc1a124
 
 
 
 
 
 
 
 
 
 
 
 
790b170
dc1a124
b26607d
 
8559a47
 
 
 
b26607d
8559a47
 
b26607d
8559a47
b26607d
8559a47
 
 
 
 
 
 
 
 
 
 
 
359511c
8559a47
 
359511c
8559a47
 
359511c
8559a47
 
359511c
8559a47
 
9dad242
 
d2ea659
9dad242
 
 
acc377c
9dad242
 
 
8559a47
b26607d
 
8559a47
 
b26607d
a490c50
b26607d
8559a47
b26607d
 
 
 
 
 
8559a47
b26607d
8559a47
 
 
b26607d
 
 
 
8559a47
ce2c8a2
b26607d
 
 
 
 
 
 
 
 
 
 
 
acc377c
9dad242
 
 
 
 
 
159120a
 
875ca32
9dad242
 
 
 
 
8c45656
9dad242
e6bd4d5
9dad242
e8bc1ec
 
 
 
9dad242
 
acc377c
875ca32
d66990e
 
 
 
 
 
29b136c
159120a
 
d66990e
 
 
 
 
 
 
cc5f18e
9dad242
29b136c
9dad242
d66990e
9dad242
29b136c
9dad242
a7a7cca
 
 
 
 
 
 
 
 
 
 
 
ce2c8a2
d66990e
9dad242
d66990e
9dad242
 
d66990e
366a31f
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import gradio as gr
import os
import subprocess
# install moviepy dependency
moviepy = subprocess.run(["pip", "install", "moviepy"])
ffmpeg = subprocess.run(["pip", "install", "ffmpeg-python"])
pipUpdate = subprocess.run(["pip", "install", "--upgrade", "pip"])
from azure.storage.blob import BlobServiceClient
import AzureBlobStorageVideo
import AzureBlobStorageAudio
from apiTest import videoAnalysis
from Moviepy import extract_audio_from_video
from Moviepy import load_json_output
from Moviepy import get_explosion_segments
from Moviepy import create_final_audio
from Moviepy import save_audio
from Moviepy import without_audio
from Moviepy import combine_video_audio
from Moviepy import save_video
from moviepy.editor import *
import json

def predict_video(input_video, input_audio=None, input_choice="Explosions"):
  global filename, file_size  # Use the global keyword to refer to the global variables
  
  # Check if the video is available
  if input_video is None:
    return [None, "Please upload a video"]

  filename = input_video.name  # Get the uploaded filename
  file_size = os.path.getsize(input_video.name)  # Get the file size in bytes

  # Loop until a valid video is uploaded
  if not filename.lower().endswith('.mp4'):
    return [None, "Error: Please upload an MP4 video file."]

  if file_size > 20 * 1024 * 1024:
    return [None, "Error: The upload exceeds file size 16MB. Please upload a smaller file."]


  #Initialize blob storage credentials
  storage_account_name = "useruploadhuggingface"
  storage_account_key = "zhrGpPBX6PVD+krncC4nVF4yoweEku/z2ErVxjLiuu/CjAVKqM5O4xlGWEyuWGxptL3mA1pv/6P4+AStjSjLEQ=="
  connection_string = f"DefaultEndpointsProtocol=https;AccountName={storage_account_name};AccountKey={storage_account_key};EndpointSuffix=core.windows.net"

  video_container_name = "useruploadhuggingfacevideo"
  audio_container_name = "useruploadhuggingfaceaudio"

  # 1. Upload user video file to azure blob storage

  videoBlobURL = AzureBlobStorageVideo.uploadUserVideoToBlobStorage(input_video, filename)
  videoSASToken = AzureBlobStorageVideo.generateSASToken(storage_account_name,video_container_name, filename, storage_account_key)
  videoSASURL = AzureBlobStorageVideo.generateSASURL(storage_account_name, video_container_name, filename, videoSASToken)

  # 1.1. Upload user audio if available

  userAudioInputFlag = False

  if input_audio is not None:
        userAudioInputFlag = True
  else:
        if (input_choice == "Explosions"):
          input_audio = os.path.join(os.path.dirname(__file__), "audio/1_seconds_haptic_audio.mp3")
          print("explosion selected")
        elif (input_choice == "Lightning and Thunder"):
          input_audio = os.path.join(os.path.dirname(__file__), "audio/8_seconds_Thunder.mp3")
          print("lightning and thunder selected")
        elif (input_choice == "Vehicle Racing"):
          input_audio = os.path.join(os.path.dirname(__file__), "audio/5_seconds_vehicle_audio.mp3")
          print("vehicle racing selected")
        else:
          input_audio = os.path.join(os.path.dirname(__file__), "audio/5_seconds_haptic_videos.mp3")
          print("default selected")

  """
  Processes the uploaded video (replace with your video analysis logic).

  Args:
      input_video: The uploaded video file object.
      input_audio (optional): The uploaded audio file object (MP3).

  Returns:
      A list containing the processed video and a message string.
  """
  responseQueryText = videoAnalysis(videoSASURL, videoSASToken, input_choice)

  #	IF method returns error: run analysis again
  if responseQueryText == """{"error":{"code":"InvalidRequest","message":"Value for indexName is invalid."}}""":
      responseQueryText = videoAnalysis(videoSASURL, videoSASToken, input_choice)

  AzureBlobStorageVideo.delete_container('useruploadhuggingfacevideo')

  json_data = load_json_output(responseQueryText)

  # Extract audio from the video
  audio_path = extract_audio_from_video(input_video)
  # Get explosion segments
  explosion_segments = get_explosion_segments(json_data)

  print(input_audio)

  # Create final audio
  #final_audio = create_final_audio(audio_path, explosion_segments)
  final_audio = create_final_audio(audio_path, input_audio, explosion_segments)
  # Save enhanced audio
  finalAudioPath = "audio/finalAudio.mp3"
  save_audio(final_audio, finalAudioPath)

  if (userAudioInputFlag == True):
      AzureBlobStorageVideo.delete_container('useruploadhuggingfaceaudio')

  # Extract video without audio
  current_video = without_audio(VideoFileClip(input_video))

  # Combine video with final audio
  final_video = combine_video_audio(current_video, final_audio)

  # Save final video
  save_video(final_video, "video/final_enhanced_video.mp4")
  finalVideoPath = "video/final_enhanced_video.mp4"

  return [finalVideoPath, f"Video enhancement successful"]

css = """
#col-container {
  margin: 0 auto;
  max-width: 800px;
}
"""
video_1 = os.path.join(os.path.dirname(__file__), "video/test_video.mp4")
audio_1 = os.path.join(os.path.dirname(__file__), "audio/audioTrack.mp3")
search_1 = "Explosions"
with gr.Blocks(css=css) as demo:
  with gr.Column(elem_id="col-container"):
    gr.HTML("""
      <h2>Phone brr</h2>
      <h3>Welcome to the Hugging Face Space of Phone brr! We aim to create more immersive content for mobile phones with the use of haptic audio, this demo focuses on working for a very commonly used special effect of explosions hope you enjoy it.</h3>

      <p>Instructions:
        <br>Step 1: Upload your MP4 video.
        <br>Step 2: (Optional) Upload an MP3 audio track.
        <br>Step 3:(Optional) Choose the instance you want haptics to be added to
        <br>Step 4: Click on submit, and We'll analyse the video and suggest explosion timeframes using Azure Cognitive Services.
        <br>Step 5: The Haptic Audio will be mixed into the video and enhanced through AI mastering.
        <br>Step 6: View and download the final videoΒ withΒ haptics.
      </p>
    """)

  with gr.Row():
    with gr.Column():
      video_in = gr.File(label="Upload a Video", file_types=[".mp4"])
      with gr.Row():
        audio_in = gr.File(label="Optional: Upload an Audio Track", file_types=[".mp3"])
    with gr.Column():
      choice_in = gr.Dropdown(
            ["Explosions", "Lightning and Thunder", "Vehicle Racing"],value=callable(""),
            label="Choose", info="Haptic Audio will be added for the selected instance in a video",
            allow_custom_value=True          
        )
      with gr.Row():
        btn_in = gr.Button("Submit", scale=0)
    with gr.Column():
      video_out = gr.Video(label="Output Video")
      with gr.Row():
        text_out = gr.Textbox(label="Output Text")

  gr.Examples(
      examples=[[video_1,audio_1]],
      fn=predict_video,
      inputs=[video_in, audio_in,choice_in],
      outputs=[video_out, text_out],
      #cache_examples=True  # Cache examples for faster loading
  )
  with gr.Column():
    gr.HTML("""
            <h3> Audio Library </h2>
            <p> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/1_seconds_haptic_audio.mp3"> Explosion Audio Track 1 </a>
            <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/5_seconds_haptic_videos.mp3"> Explosion Audio Track 2 </a>
            <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/6_seconds_haptic_audio.mp3"> Explosion Audio Track 3 </a>
            <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/7_seconds_haptic_audio.mp3"> Explosion Audio Track 4 </a>
            <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/9_seconds_haptic_videos.mp3"> Explosion Audio Track 5 </a>
            <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/5_seconds_vehicle_audio.mp3"> Vehicle Audio Track 1 </a>
            <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/30_seconds_vehicle_audio.mp3"> Vehicle Audio Track 2 </a>
            </p>
            """)
    
  btn_in.click(
      fn=predict_video,
      inputs=[video_in,audio_in,choice_in],
      outputs=[video_out, text_out],
      queue=False
  )
demo.launch(debug=True)