From 9cc4d1c526100a4a20f6c8f0ff842459c1611094 Mon Sep 17 00:00:00 2001 From: Owen-Cochell Date: Wed, 12 Apr 2023 17:22:50 -0400 Subject: [PATCH] Added time interval merging --- static/js/transcript-editing.js | 2 +- transcription/views.py | 73 +++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/static/js/transcript-editing.js b/static/js/transcript-editing.js index b25b9d2..b78e6e4 100755 --- a/static/js/transcript-editing.js +++ b/static/js/transcript-editing.js @@ -88,7 +88,7 @@ function deleteTranscript() { // if any word has been selected, then update the video selectedWordsArr.forEach(element => { console.log(element); - timestamps["timestamps"].push([element.getAttribute("data-start"), element.getAttribute("data-stop")]); + timestamps["timestamps"].push([parseFloat(element.getAttribute("data-start")), parseFloat(element.getAttribute("data-stop"))]); element.remove(); }); diff --git a/transcription/views.py b/transcription/views.py index 7008838..930b4a8 100644 --- a/transcription/views.py +++ b/transcription/views.py @@ -10,12 +10,67 @@ import whisper_timestamped as whisper import moviepy.editor +SMALL = 0.1 # Number of seconds for two cuts to count as one # Load a model: model = whisper.load_model("base") +def merge_times(times): + """ + Merges a list of times. + + The idea is to convert two cuts with a small + time diff into one cut to avoid awkward cuts. + + :param times: List of times + :type times: List[Tuple(int, int)] + """ + + final = [] + last = [0, 0] + index = -1 + + # Iterate over all times: + + while index < len(times) - 1: + + # Determine if we need to merge + + if times[index+1][0] - last[1] < SMALL: + + # Set last: + + last = [last[0], times[index+1][1]] + + # Otherwise, just set last: + + else: + + # Determine if we should add to final: + + if last[0] != last[1]: + + # Add value to final: + + final.append(last) + + # Set last: + + last = times[index+1] + + # Increment index: + + index += 1 + + # Finally, add last to list: + + final.append(last) + + return final + + def index(request): return render(request, 'Frontend/Home.html') @@ -174,7 +229,19 @@ def cut_files(request): # Decode the cuts: - cuts = json.loads(request.POST['cuts']) + cuts = json.loads(request.POST['cuts'])['timestamps'] + + # Sort by start value: + + cuts.sort(key=lambda x: x[0]) + + print("Original Cut: {}".format(cuts)) + + if len(cuts) >= 2: + + cuts = merge_times(cuts) + + print(cuts) # Create video model and blank file: @@ -190,8 +257,8 @@ def cut_files(request): # Iterate over each cut: - for cut in cuts['timestamps']: - cut = [eval(i) for i in cut] + for cut in cuts: + print(f"Current timestamp: {cut}") clip = clip.cutout(cut[0] - total_removed, cut[1] - total_removed)