Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Make video recording work on Safari #145

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions tools/sense_studio/static/video_recording.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,22 @@ function setupRecording(stream, url) {


function startRecording(stream, recordingDuration, url) {
let mediaRecorder = new MediaRecorder(stream, {mimeType: 'video/webm; codecs=vp8'});
let mimeType;
if (MediaRecorder.isTypeSupported('video/webm; codecs=vp8')) {
mimeType = 'video/webm;codecs=vp8';
videoType = 'webm';
} else if (MediaRecorder.isTypeSupported('video/mp4')) {
mimeType = 'video/mp4';
videoType = 'mp4';
} else {
displayOverlay('No supported video format detected. Please try using Firefox, Chrome or Safari.');
return;
}

let mediaRecorder = new MediaRecorder(stream, {mimeType: mimeType});
mediaRecorder.ondataavailable = function (event) {
if (event.data.size > 0) {
saveVideo(event.data, url);
saveVideo(event.data, url, videoType);
}
};
mediaRecorder.start();
Expand All @@ -107,10 +119,11 @@ function stopRecording(mediaRecorder) {
}


function saveVideo(chunk, url) {
let blob = new Blob([chunk], {type: 'video/webm'});
function saveVideo(chunk, url, videoType) {
let blob = new Blob([chunk], {type: 'video/' + videoType});
const formData = new FormData();
formData.append('video', blob);
formData.append('video_type', videoType);
fetch(url, {
method: 'POST',
body: formData,
Expand Down
8 changes: 6 additions & 2 deletions tools/sense_studio/video_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ def save_video(project, split, label):
label = urllib.parse.unquote(label)
path = project_utils.lookup_project_path(project)

data = request.form
video_type = data['video_type']

# Read given video to a file
input_stream = request.files['video']
output_path = os.path.join(path, f'videos_{split}', label)
temp_file_name = os.path.join(output_path, 'temp_video.webm')
temp_file_name = os.path.join(output_path, f'temp_video.{video_type}')
with open(temp_file_name, 'wb') as temp_file:
temp_file.write(input_stream.read())

Expand All @@ -59,7 +62,8 @@ def save_video(project, split, label):
output_file = os.path.join(output_path, f'video_{video_idx}.mp4')

# Convert video to target frame rate and save to output name
subprocess.call(f'ffmpeg -i "{temp_file_name}" -r 30 "{output_file}"', shell=True)
# subprocess.call(f'ffmpeg -i "{temp_file_name}" -r 30 "{output_file}"', shell=True)
subprocess.call(f'cp "{temp_file_name}" "{output_file}"', shell=True)

# Remove temp video file
os.remove(temp_file_name)
Expand Down