SE-09/HapticsProject
2
1import gradio as gr2import os3import subprocess4# install moviepy dependency5moviepy = subprocess.run(["pip", "install", "moviepy"])6ffmpeg = subprocess.run(["pip", "install", "ffmpeg-python"])7pipUpdate = subprocess.run(["pip", "install", "--upgrade", "pip"])8from azure.storage.blob import BlobServiceClient9import AzureBlobStorageVideo10import AzureBlobStorageAudio11from apiTest import videoAnalysis12from Moviepy import extract_audio_from_video13from Moviepy import load_json_output14from Moviepy import get_explosion_segments15from Moviepy import create_final_audio16from Moviepy import save_audio17from Moviepy import without_audio18from Moviepy import combine_video_audio19from Moviepy import save_video20from moviepy.editor import *21import json22 23def predict_video(input_video, input_audio=None, input_choice="Explosions"):24 global filename, file_size # Use the global keyword to refer to the global variables25 26 # Check if the video is available27 if input_video is None:28 return [None, "Please upload a video"]29 30 filename = input_video.name # Get the uploaded filename31 file_size = os.path.getsize(input_video.name) # Get the file size in bytes32 33 # Loop until a valid video is uploaded34 if not filename.lower().endswith('.mp4'):35 return [None, "Error: Please upload an MP4 video file."]36 37 if file_size > 20 * 1024 * 1024:38 return [None, "Error: The upload exceeds file size 16MB. Please upload a smaller file."]39 40 41 #Initialize blob storage credentials42 storage_account_name = "useruploadhuggingface"43 storage_account_key = "zhrGpPBX6PVD+krncC4nVF4yoweEku/z2ErVxjLiuu/CjAVKqM5O4xlGWEyuWGxptL3mA1pv/6P4+AStjSjLEQ=="44 connection_string = f"DefaultEndpointsProtocol=https;AccountName={storage_account_name};AccountKey={storage_account_key};EndpointSuffix=core.windows.net"45 46 video_container_name = "useruploadhuggingfacevideo"47 audio_container_name = "useruploadhuggingfaceaudio"48 49 # 1. Upload user video file to azure blob storage50 51 videoBlobURL = AzureBlobStorageVideo.uploadUserVideoToBlobStorage(input_video, filename)52 videoSASToken = AzureBlobStorageVideo.generateSASToken(storage_account_name,video_container_name, filename, storage_account_key)53 videoSASURL = AzureBlobStorageVideo.generateSASURL(storage_account_name, video_container_name, filename, videoSASToken)54 55 # 1.1. Upload user audio if available56 57 userAudioInputFlag = False58 59 if input_audio is not None:60 userAudioInputFlag = True61 else:62 if (input_choice == "Explosions"):63 input_audio = os.path.join(os.path.dirname(__file__), "audio/1_seconds_haptic_audio.mp3")64 print("explosion selected")65 elif (input_choice == "Lightning and Thunder"):66 input_audio = os.path.join(os.path.dirname(__file__), "audio/8_seconds_Thunder.mp3")67 print("lightning and thunder selected")68 elif (input_choice == "Vehicle Racing"):69 input_audio = os.path.join(os.path.dirname(__file__), "audio/5_seconds_vehicle_audio.mp3")70 print("vehicle racing selected")71 else:72 input_audio = os.path.join(os.path.dirname(__file__), "audio/5_seconds_haptic_videos.mp3")73 print("default selected")74 75 """76 Processes the uploaded video (replace with your video analysis logic).77 78 Args:79 input_video: The uploaded video file object.80 input_audio (optional): The uploaded audio file object (MP3).81 82 Returns:83 A list containing the processed video and a message string.84 """85 responseQueryText = videoAnalysis(videoSASURL, videoSASToken, input_choice)86 87 # IF method returns error: run analysis again88 if responseQueryText == """{"error":{"code":"InvalidRequest","message":"Value for indexName is invalid."}}""":89 responseQueryText = videoAnalysis(videoSASURL, videoSASToken, input_choice)90 91 AzureBlobStorageVideo.delete_container('useruploadhuggingfacevideo')92 93 json_data = load_json_output(responseQueryText)94 95 # Extract audio from the video96 audio_path = extract_audio_from_video(input_video)97 # Get explosion segments98 explosion_segments = get_explosion_segments(json_data)99 100 print(input_audio)101 102 # Create final audio103 #final_audio = create_final_audio(audio_path, explosion_segments)104 final_audio = create_final_audio(audio_path, input_audio, explosion_segments)105 # Save enhanced audio106 finalAudioPath = "audio/finalAudio.mp3"107 save_audio(final_audio, finalAudioPath)108 109 if (userAudioInputFlag == True):110 AzureBlobStorageVideo.delete_container('useruploadhuggingfaceaudio')111 112 # Extract video without audio113 current_video = without_audio(VideoFileClip(input_video))114 115 # Combine video with final audio116 final_video = combine_video_audio(current_video, final_audio)117 118 # Save final video119 save_video(final_video, "video/final_enhanced_video.mp4")120 finalVideoPath = "video/final_enhanced_video.mp4"121 122 return [finalVideoPath, f"Video enhancement successful"]123 124css = """125#col-container {126 margin: 0 auto;127 max-width: 800px;128}129"""130video_1 = os.path.join(os.path.dirname(__file__), "video/test_video.mp4")131audio_1 = os.path.join(os.path.dirname(__file__), "audio/audioTrack.mp3")132search_1 = "Explosions"133with gr.Blocks(css=css) as demo:134 with gr.Column(elem_id="col-container"):135 gr.HTML("""136 <h2>Phone brr</h2>137 <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>138 139 <p>Instructions:140 <br>Step 1: Upload your MP4 video.141 <br>Step 2: (Optional) Upload an MP3 audio track.142 <br>Step 3:(Optional) Choose the instance you want haptics to be added to143 <br>Step 4: Click on submit, and We'll analyse the video and suggest explosion timeframes using Azure Cognitive Services.144 <br>Step 5: The Haptic Audio will be mixed into the video and enhanced through AI mastering.145 <br>Step 6: View and download the final video with haptics.146 </p>147 """)148 149 with gr.Row():150 with gr.Column():151 video_in = gr.File(label="Upload a Video", file_types=[".mp4"])152 with gr.Row():153 audio_in = gr.File(label="Optional: Upload an Audio Track", file_types=[".mp3"])154 with gr.Column():155 choice_in = gr.Dropdown(156 ["Explosions", "Lightning and Thunder", "Vehicle Racing"],value=callable(""),157 label="Choose", info="Haptic Audio will be added for the selected instance in a video",158 allow_custom_value=True 159 )160 with gr.Row():161 btn_in = gr.Button("Submit", scale=0)162 with gr.Column():163 video_out = gr.Video(label="Output Video")164 with gr.Row():165 text_out = gr.Textbox(label="Output Text")166 167 gr.Examples(168 examples=[[video_1,audio_1]],169 fn=predict_video,170 inputs=[video_in, audio_in,choice_in],171 outputs=[video_out, text_out],172 #cache_examples=True # Cache examples for faster loading173 )174 with gr.Column():175 gr.HTML("""176 <h3> Audio Library </h2>177 <p> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/1_seconds_haptic_audio.mp3"> Explosion Audio Track 1 </a>178 <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/5_seconds_haptic_videos.mp3"> Explosion Audio Track 2 </a>179 <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/6_seconds_haptic_audio.mp3"> Explosion Audio Track 3 </a>180 <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/7_seconds_haptic_audio.mp3"> Explosion Audio Track 4 </a>181 <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/9_seconds_haptic_videos.mp3"> Explosion Audio Track 5 </a>182 <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/5_seconds_vehicle_audio.mp3"> Vehicle Audio Track 1 </a>183 <br> <a href="https://audiolibrary.blob.core.windows.net/audiolibrary/30_seconds_vehicle_audio.mp3"> Vehicle Audio Track 2 </a>184 </p>185 """)186 187 btn_in.click(188 fn=predict_video,189 inputs=[video_in,audio_in,choice_in],190 outputs=[video_out, text_out],191 queue=False192 )193demo.launch(debug=True)