Spaces:
Running
Running
import base64 | |
import pathlib | |
import tempfile | |
import gradio as gr | |
recorder_js = pathlib.Path('recorder.js').read_text() | |
main_js = pathlib.Path('main.js').read_text() | |
record_button_js = pathlib.Path('record_button.js').read_text().replace('let recorder_js = null;', recorder_js).replace( | |
'let main_js = null;', main_js) | |
def save_base64_video(base64_string): | |
base64_video = base64_string | |
video_data = base64.b64decode(base64_video) | |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as temp_file: | |
temp_filename = temp_file.name | |
temp_file.write(video_data) | |
print(f"Temporary MP4 file saved as: {temp_filename}") | |
return temp_filename | |
with gr.Blocks(title="Screen Recorder Demo") as demo: | |
start_button = gr.Button("Record Screen π΄") | |
video_component = gr.Video(interactive=True, show_share_button=True) | |
def toggle_button_label(returned_string): | |
if returned_string.startswith("Record"): | |
return gr.Button(value="Stop Recording βͺ"), None | |
else: | |
try: | |
temp_filename = save_base64_video(returned_string) | |
except Exception as e: | |
return gr.Button(value="Record Screen π΄"), gr.Warning(f'Failed to convert video to mp4:\n{e}') | |
return gr.Button(value="Record Screen π΄"), gr.Video(value=temp_filename, interactive=True, | |
show_share_button=True) | |
start_button.click(toggle_button_label, start_button, [start_button, video_component], js=record_button_js) | |
if __name__ == "__main__": | |
demo.launch() | |