Skip to content

Commit

Permalink
Some comments and changes for readability #1266
Browse files Browse the repository at this point in the history
  • Loading branch information
Jetske committed Jul 2, 2024
1 parent f5bbf5e commit 651f13b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,26 @@
<br><br>
<input type="checkbox" id="trim-video-checked" name="trim-video-checked" role="button" value="off"> {% trans 'Shorten the video' %}
<div id="trim_video">
{% trans "Instructions: slide to a point in the video and set it as start and/or end point" %}<br>
<button type="button" id="start-button" class="btn btn-primary" data-dismiss="modal">{% trans "Set new start point" %}</button>
Start: <input type="text" id="start-point" name="start-ms" value="0" readonly><br>
<button type="button" id="end-button" class="btn btn-primary" data-dismiss="modal">{% trans "Set new end point" %}</button>
End: <input type="text" id="end-point" name="end-ms" value="{{ annotated_sentence.annotatedvideo.get_end_ms }}" readonly><br>
<button type="button" id="reset-trim" class="btn btn-primary" data-dismiss="modal">{% trans "Reset" %}</button>
<span id="trim-feedback"></span>
{% trans "Slide to a point in the video and click 'Set' to define it as the new start and/or end point" %}<br>
<table class='table' style="width:70%">
<tr>
<td style="width:50%">
Start: <input type="text" id="start-point" name="start-ms" value="0" readonly>
<button type="button" id="start-button" class="btn btn-primary" data-dismiss="modal">{% trans "Set" %}</button>
</td>
<td style="width:50%">
End: <input type="text" id="end-point" name="end-ms" value="{{ annotated_sentence.annotatedvideo.get_end_ms }}" readonly>
<button type="button" id="end-button" class="btn btn-primary" data-dismiss="modal">{% trans "Set" %}</button>
</td>
</tr>
<tr>
<td colspan="2">
<button type="button" id="reset-trim" class="btn btn-primary" data-dismiss="modal">{% trans "Reset" %}</button>
<span id="trim-feedback"></span>
</td>
</tr>
</table>

</div>

<style>
Expand All @@ -65,7 +78,7 @@
var currentTime = (video.currentTime * 1000).toFixed(0);
var endValue = parseInt(endPoint.value, 10);
if (!isNaN(endValue) && currentTime > endValue) {
feedback.innerHTML = "Start point must be before end point";
feedback.innerHTML = '<font color="red">Start point must be before end point</font>';
} else {
startPoint.value = currentTime;
feedback.innerHTML = "";
Expand All @@ -76,7 +89,7 @@
var currentTime = (video.currentTime * 1000).toFixed(0);
var startValue = parseInt(startPoint.value, 10);
if (!isNaN(startValue) && currentTime < startValue) {
feedback.innerHTML = "End point must be after start point";
feedback.innerHTML = '<font color="red">End point must be after start point</font>';
} else {
endPoint.value = currentTime;
feedback.innerHTML = "";
Expand Down
2 changes: 1 addition & 1 deletion signbank/dictionary/templates/dictionary/gloss_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ <h2 id='modalTitleSimilarSense'>{% trans "Similar senses" %}</h2>
<span class = "sense-icon pull-right"><a href='{% url "dictionary:add_sentence_video" glossid=gloss.id examplesentenceid=examplesentence.id %}'><img src='{{STATIC_URL}}images/addvideo.png' style="width:25px; height:25px; margin: 0px; cursor: pointer;"></a></span>
{% if examplesentence.has_video %}
<span class="sense-icon pull-right">
<form class="formbutton" action="{{PREFIX_URL}}/video/deletesentence_video/{{examplesentence.id}}" method='post'>
<form class="formbutton" action="{{PREFIX_URL}}/video/deletesentencevideo/{{examplesentence.id}}" method='post'>
{% csrf_token %}
<input type=image value="" src='{{STATIC_URL}}images/deletevideo.png' alt = "submit" style='width: 25px; height: 25px; margin: 0px; cursor: pointer;'>
</form>
Expand Down
2 changes: 1 addition & 1 deletion signbank/dictionary/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
re_path(r'^update/examplesentence/(?P<examplesentenceid>\d+)$', signbank.dictionary.update.update_examplesentence, name='update_examplesentence'),
re_path(r'^update/addexamplesentence/(?P<senseid>\d+)$', signbank.dictionary.update.create_examplesentence, name='create_examplesentence'),
re_path(r'^update/deleteexamplesentence/(?P<senseid>\d+)$', signbank.dictionary.update.delete_examplesentence, name='delete_examplesentence'),
re_path(r'^update/addsentence-video/(?P<glossid>\d+)/(?P<examplesentenceid>\d+)$', signbank.dictionary.update.add_sentence_video, name='add_sentence_video'),
re_path(r'^update/addsentencevideo/(?P<glossid>\d+)/(?P<examplesentenceid>\d+)$', signbank.dictionary.update.add_sentence_video, name='add_sentence_video'),
re_path(r'^update/addannotatedmedia/(?P<glossid>\d+)$', signbank.dictionary.update.add_annotated_media, name='add_annotated_media'),
re_path(r'^update/updateannotatedsentence/(?P<glossid>\d+)/(?P<annotatedsentenceid>\d+)$', signbank.dictionary.update.edit_annotated_sentence, name='edit_annotated_sentence'),
re_path(r'^update/saveeditannotatedsentence/', signbank.dictionary.update.save_edit_annotated_sentence, name='save_edit_annotated_sentence'),
Expand Down
39 changes: 13 additions & 26 deletions signbank/video/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,16 @@ def __str__(self):
return self.videofile.name

def convert_milliseconds_to_time_format(self, ms):
"""Convert milliseconds to a time format HH:MM:SS.mmm"""
milliseconds = ms % 1000
seconds = (ms // 1000) % 60
minutes = (ms // (1000 * 60)) % 60
hours = (ms // (1000 * 60 * 60)) % 24
return f"{hours:02}:{minutes:02}:{seconds:02}.{milliseconds:03}"

def select_annotations(self, eaf, tier_name, start_ms, end_ms):
""" Select annotations that are within the selected range """

keys_to_remove = []
for key in eaf.tiers[tier_name][0]:
annotation_list = list(eaf.tiers[tier_name][0][key])
Expand All @@ -529,66 +532,50 @@ def select_annotations(self, eaf, tier_name, start_ms, end_ms):
del eaf.tiers[tier_name][0][key]

def cut_video_and_eaf(self, start_ms, end_ms):
# cut both the video and the eaffile annotations
"""cut both the video and the annotation file (eaf) to the selected range"""

import subprocess
from pathlib import Path
from pympi.Elan import Eaf

start_ms, end_ms = int(start_ms), int(end_ms)
duration_ms = end_ms - start_ms
start_time = self.convert_milliseconds_to_time_format(start_ms)
end_time = self.convert_milliseconds_to_time_format(end_ms)
duration_time = self.convert_milliseconds_to_time_format(duration_ms)

from pathlib import Path
# Cut the video
input_file = Path(self.videofile.path)
temp_output_file = Path(os.path.join(os.path.split(input_file)[0], 'temp.mp4'))
command = [
'ffmpeg',
'-i', input_file,
'-ss', start_time, # Seek to the start time
'-to', end_time, # Specify the duration
'-c:v', 'libx264',
'-c:a', 'aac',
'-y', # Overwrite the output file without asking
temp_output_file
]

# Open subprocess
command = ['ffmpeg', '-i', input_file, '-ss', start_time, '-to', end_time, '-c:v', 'libx264', '-c:a', 'aac', '-y', temp_output_file]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Communicate and wait for process to finish
stdout, stderr = process.communicate()
_, stderr = process.communicate()
stderr_str = stderr.decode('utf-8')

if process.returncode != 0:
raise RuntimeError(f"ffmpeg error: {stderr_str}")
else:
# Overwrite the original file with the cut video
overwrite_command = ['mv', temp_output_file, input_file]
process = subprocess.Popen(overwrite_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
_, stderr = process.communicate()
stderr_str = stderr.decode('utf-8')

if process.returncode != 0:
raise RuntimeError(f"File overwrite error: {stderr_str}")

# Cut the eaf
eaf = Eaf(self.eaffile.path)
eaf.timeslots['ts1000'] = start_ms
eaf.timeslots['ts1001'] = end_ms

self.select_annotations(eaf, 'Sentences', start_ms, end_ms)
self.select_annotations(eaf, 'Glosses R', start_ms, end_ms)
self.select_annotations(eaf, 'Glosses L', start_ms, end_ms)

# shift the timeslots to start at 0
for key in eaf.timeslots:
eaf.timeslots[key] -= start_ms
eaf.clean_time_slots()

# link the new video file
eaf.remove_linked_files()
eaf.remove_secondary_linked_files()
relpath = os.path.split(Path(self.videofile.path))[1]
eaf.add_linked_file(str(self.videofile.path), str(relpath), 'video/mp4', 0)

eaf.to_file(self.eaffile.path)


Expand Down
2 changes: 1 addition & 1 deletion signbank/video/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
re_path(r'^video/(?P<videoid>\d+)$', signbank.video.views.video),
re_path(r'^upload/', signbank.video.views.addvideo),
re_path(r'^delete/(?P<videoid>\d+)$', signbank.video.views.deletevideo),
re_path(r'^deletesentence_video/(?P<videoid>\d+)$', signbank.video.views.deletesentence_video),
re_path(r'^deletesentencevideo/(?P<videoid>\d+)$', signbank.video.views.deletesentencevideo),
re_path(r'^process_eaffile/', signbank.video.views.process_eaffile, name='process_eaffile'),
re_path(r'^create_still_images/', permission_required('dictionary.change_gloss')(signbank.video.views.create_still_images))
]
2 changes: 1 addition & 1 deletion signbank/video/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def process_eaffile(request):


@login_required
def deletesentence_video(request, videoid):
def deletesentencevideo(request, videoid):
"""Remove the video for this gloss, if there is an older version
then reinstate that as the current video (act like undo)"""

Expand Down

0 comments on commit 651f13b

Please sign in to comment.