• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EsupPortail / Esup-Pod / 6377635546

02 Oct 2023 08:21AM UTC coverage: 70.396% (-1.6%) from 71.99%
6377635546

push

github

web-flow
Merge pull request #900 from EsupPortail/develop

[DONE] #3.4.0

1509 of 1509 new or added lines in 58 files covered. (100.0%)

9288 of 13194 relevant lines covered (70.4%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.14
/pod/video_encode_transcript/encode.py
1
"""This module handles video encoding with CPU."""
2

3
from django.conf import settings
1✔
4
from webpush.models import PushInformation
1✔
5

6
from pod.video.models import Video
1✔
7
from .Encoding_video_model import Encoding_video_model
1✔
8
from .encoding_studio import encode_video_studio
1✔
9

10
from pod.cut.models import CutVideo
1✔
11
from pod.main.tasks import task_start_encode, task_start_encode_studio
1✔
12
from .utils import (
1✔
13
    change_encoding_step,
14
    check_file,
15
    add_encoding_log,
16
    send_email,
17
    send_email_encoding,
18
    send_notification_encoding,
19
    time_to_seconds,
20
)
21
import logging
1✔
22
import time
1✔
23
import threading
1✔
24

25
__license__ = "LGPL v3"
1✔
26
log = logging.getLogger(__name__)
1✔
27

28
USE_TRANSCRIPTION = getattr(settings, "USE_TRANSCRIPTION", False)
1✔
29

30
if USE_TRANSCRIPTION:
1✔
31
    from . import transcript
×
32

33
    TRANSCRIPT_VIDEO = getattr(settings, "TRANSCRIPT_VIDEO", "start_transcript")
×
34

35
CELERY_TO_ENCODE = getattr(settings, "CELERY_TO_ENCODE", False)
1✔
36
EMAIL_ON_ENCODING_COMPLETION = getattr(settings, "EMAIL_ON_ENCODING_COMPLETION", True)
1✔
37

38
USE_DISTANT_ENCODING_TRANSCODING = getattr(
1✔
39
    settings, "USE_DISTANT_ENCODING_TRANSCODING", False
40
)
41
if USE_DISTANT_ENCODING_TRANSCODING:
1✔
42
    from .encoding_tasks import start_encoding_task
×
43

44
# ##########################################################################
45
# ENCODE VIDEO: THREAD TO LAUNCH ENCODE
46
# ##########################################################################
47

48
# Disable for the moment, will be reactivated in future version
49
"""
50
def start_remote_encode(video_id):
51
    # load module here to prevent circular import
52
    from .remote_encode import remote_encode_video
53

54
    log.info("START ENCODE VIDEO ID %s" % video_id)
55
    t = threading.Thread(target=remote_encode_video, args=[video_id])
56
    t.setDaemon(True)
57
    t.start()
58
"""
59

60

61
def start_encode(video_id, threaded=True):
1✔
62
    """Start local encoding."""
63
    if threaded:
1✔
64
        if CELERY_TO_ENCODE:
1✔
65
            task_start_encode.delay(video_id)
×
66
        else:
67
            log.info("START ENCODE VIDEO ID %s" % video_id)
1✔
68
            t = threading.Thread(target=encode_video, args=[video_id])
1✔
69
            t.setDaemon(True)
1✔
70
            t.start()
1✔
71
    else:
72
        encode_video(video_id)
×
73

74

75
def start_encode_studio(recording_id, video_output, videos, subtime, presenter):
1✔
76
    """Start local encoding."""
77
    if CELERY_TO_ENCODE:
×
78
        task_start_encode_studio.delay(
×
79
            recording_id, video_output, videos, subtime, presenter
80
        )
81
    else:
82
        log.info("START ENCODE VIDEO ID %s" % recording_id)
×
83
        t = threading.Thread(
×
84
            target=encode_video_studio,
85
            args=[recording_id, video_output, videos, subtime, presenter],
86
        )
87
        t.setDaemon(True)
×
88
        t.start()
×
89

90

91
"""
92
def start_studio_remote_encode(recording_id, video_output, videos, subtime, presenter):
93
    # load module here to prevent circular import
94
    from .remote_encode import remote_encode_studio
95

96
    log.info("START ENCODE RECORDING ID %s" % recording_id)
97
    t = threading.Thread(
98
        target=remote_encode_studio,
99
        args=[recording_id, video_output, videos, subtime, presenter],
100
    )
101
    t.setDaemon(True)
102
    t.start()
103
"""
104

105

106
def encode_video(video_id):
1✔
107
    """ENCODE VIDEO: MAIN FUNCTION."""
108
    start = "Start at: %s" % time.ctime()
1✔
109

110
    video_to_encode = Video.objects.get(id=video_id)
1✔
111
    video_to_encode.encoding_in_progress = True
1✔
112
    video_to_encode.save()
1✔
113

114
    if not check_file(video_to_encode.video.path):
1✔
115
        msg = "Wrong file or path:" + "\n%s" % video_to_encode.video.path
1✔
116
        add_encoding_log(video_id, msg)
1✔
117
        change_encoding_step(video_id, -1, msg)
1✔
118
        send_email(msg, video_id)
1✔
119
        return
1✔
120

121
    change_encoding_step(video_id, 0, "start")
1✔
122
    # start and stop cut ?
123
    encoding_video = get_encoding_video(video_to_encode)
1✔
124
    encoding_video.add_encoding_log("start_time", "", True, start)
1✔
125
    change_encoding_step(video_id, 1, "remove old data")
1✔
126
    encoding_video.remove_old_data()
1✔
127

128
    change_encoding_step(video_id, 2, "start encoding")
1✔
129
    if USE_DISTANT_ENCODING_TRANSCODING:
1✔
130
        start_encoding_task.delay(
×
131
            encoding_video.id,
132
            encoding_video.video_file,
133
            encoding_video.cutting_start,
134
            encoding_video.cutting_stop,
135
        )
136
    else:
137
        encoding_video.start_encode()
1✔
138
        final_video = store_encoding_info(video_id, encoding_video)
1✔
139
        end_of_encoding(final_video)
1✔
140

141

142
def store_encoding_info(video_id, encoding_video):
1✔
143
    """Store all encoding file and informations from encoding tasks."""
144
    change_encoding_step(video_id, 3, "store encoding info")
1✔
145
    final_video = encoding_video.store_json_info()
1✔
146
    final_video.is_video = final_video.get_video_m4a() is None
1✔
147
    final_video.encoding_in_progress = False
1✔
148
    final_video.save()
1✔
149
    return final_video
1✔
150

151

152
def get_encoding_video(video_to_encode):
1✔
153
    """Get the encoding video object from video."""
154
    if CutVideo.objects.filter(video=video_to_encode).exists():
1✔
155
        cut = CutVideo.objects.get(video=video_to_encode)
×
156
        cut_start = time_to_seconds(cut.start)
×
157
        cut_end = time_to_seconds(cut.end)
×
158
        encoding_video = Encoding_video_model(
×
159
            video_to_encode.id, video_to_encode.video.path, cut_start, cut_end
160
        )
161
        return encoding_video
×
162
    return Encoding_video_model(video_to_encode.id, video_to_encode.video.path)
1✔
163

164

165
def end_of_encoding(video):
1✔
166
    """Send mail at the end of encoding, call transcription."""
167
    if (
1✔
168
        video.owner.owner.accepts_notifications
169
        and PushInformation.objects.filter(user=video.owner).exists()
170
    ):
171
        send_notification_encoding(video)
×
172
    elif EMAIL_ON_ENCODING_COMPLETION:
1✔
173
        send_email_encoding(video)
1✔
174

175
    transcript_video(video.id)
1✔
176
    change_encoding_step(video.id, 0, "end of encoding")
1✔
177

178

179
def transcript_video(video_id):
1✔
180
    """Transcript video audio to text."""
181
    video = Video.objects.get(id=video_id)
1✔
182
    if USE_TRANSCRIPTION and video.transcript not in ["", "0", "1"]:
1✔
183
        change_encoding_step(video_id, 4, "transcript video")
×
184
        start_transcript_video = getattr(transcript, TRANSCRIPT_VIDEO)
×
185
        start_transcript_video(video_id, False)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc