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

EsupPortail / Esup-Pod / 22617135523

03 Mar 2026 09:41AM UTC coverage: 68.067%. First build
22617135523

Pull #1407

github

web-flow
Merge branch 'main' into dev_v4
Pull Request #1407: [RELEASE] Pod 4.2.0

736 of 1596 new or added lines in 32 files covered. (46.12%)

12915 of 18974 relevant lines covered (68.07%)

0.68 hits per line

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

71.53
/pod/video_encode_transcript/encode.py
1
"""Esup-Pod module to handle video encoding with CPU."""
2

3
import logging
1✔
4
import threading
1✔
5
import time
1✔
6

7
from django.conf import settings
1✔
8
from webpush.models import PushInformation
1✔
9

10
from pod.cut.models import CutVideo
1✔
11
from pod.dressing.models import Dressing
1✔
12
from pod.dressing.utils import get_dressing_input
1✔
13
from pod.main.tasks import task_start_encode, task_start_encode_studio
1✔
14
from pod.recorder.models import Recording
1✔
15
from pod.video.models import Video
1✔
16

17
from .encoding_settings import FFMPEG_DRESSING_INPUT
1✔
18
from .encoding_studio import start_encode_video_studio
1✔
19
from .Encoding_video_model import Encoding_video_model
1✔
20
from .models import EncodingLog
1✔
21
from .utils import (
1✔
22
    add_encoding_log,
23
    change_encoding_step,
24
    check_file,
25
    send_email,
26
    send_email_encoding,
27
    send_email_recording,
28
    send_notification_encoding,
29
    time_to_seconds,
30
)
31

32
__license__ = "LGPL v3"
1✔
33
log = logging.getLogger(__name__)
1✔
34

35
USE_TRANSCRIPTION = getattr(settings, "USE_TRANSCRIPTION", False)
1✔
36
USE_NOTIFICATIONS = getattr(settings, "USE_NOTIFICATIONS", False)
1✔
37
if USE_TRANSCRIPTION:
1✔
38
    from . import transcript
1✔
39

40
    TRANSCRIPT_VIDEO = getattr(settings, "TRANSCRIPT_VIDEO", "start_transcript")
1✔
41

42
CELERY_TO_ENCODE = getattr(settings, "CELERY_TO_ENCODE", False)
1✔
43
EMAIL_ON_ENCODING_COMPLETION = getattr(settings, "EMAIL_ON_ENCODING_COMPLETION", True)
1✔
44

45
USE_REMOTE_ENCODING_TRANSCODING = getattr(
1✔
46
    settings, "USE_REMOTE_ENCODING_TRANSCODING", False
47
)
48
if USE_REMOTE_ENCODING_TRANSCODING:
1✔
49
    from .encoding_tasks import start_encoding_task, start_studio_task
1✔
50

51
FFMPEG_DRESSING_INPUT = getattr(settings, "FFMPEG_DRESSING_INPUT", FFMPEG_DRESSING_INPUT)
1✔
52

53
USE_RUNNER_MANAGER = getattr(settings, "USE_RUNNER_MANAGER", False)
1✔
54

55
# ##########################################################################
56
# ENCODE VIDEO: THREAD TO LAUNCH ENCODE
57
# ##########################################################################
58

59

60
def start_encode(video_id: int, threaded=True) -> None:
1✔
61
    """Start video encoding."""
62
    # Special case for runner manager: delegate encoding to a remote service and return immediately without threading logic
63
    if USE_RUNNER_MANAGER:
1✔
NEW
64
        log.info("Start encode, with runner manager, for id: %s" % video_id)
×
65
        # Load module here to prevent circular import
NEW
66
        from .runner_manager import encode_video as runner_encode_video
×
67

NEW
68
        runner_encode_video(video_id)
×
69
    else:
70
        log.info("Start encode, without runner manager, for id: %s" % video_id)
1✔
71
        if threaded:
1✔
72
            if CELERY_TO_ENCODE:
1✔
NEW
73
                task_start_encode.delay(video_id)
×
74
            else:
75
                log.info("START ENCODE VIDEO ID %s" % video_id)
1✔
76
                t = threading.Thread(target=encode_video, args=[video_id])
1✔
77
                t.daemon = True
1✔
78
                t.start()
1✔
79
        else:
80
            encode_video(video_id)
1✔
81

82

83
def start_encode_studio(
1✔
84
    recording_id, video_output, videos, subtime, presenter, threaded=True
85
) -> None:
86
    """Start studio encoding."""
87
    # Special case for runner manager: delegate encoding to a remote service and return immediately without threading logic
NEW
88
    if USE_RUNNER_MANAGER:
×
NEW
89
        log.info("Start encode studio, with runner manager, for id: %s" % recording_id)
×
90
        # Load module here to prevent circular import
NEW
91
        from .runner_manager import encode_studio_recording
×
92

NEW
93
        encode_studio_recording(recording_id)
×
94
    else:
NEW
95
        log.info("Start encode studio, without runner manager, for id: %s" % recording_id)
×
NEW
96
        if threaded:
×
NEW
97
            if CELERY_TO_ENCODE:
×
NEW
98
                task_start_encode_studio.delay(
×
99
                    recording_id, video_output, videos, subtime, presenter
100
                )
101
            else:
NEW
102
                log.info("START ENCODE VIDEO ID %s" % recording_id)
×
NEW
103
                t = threading.Thread(
×
104
                    target=encode_video_studio,
105
                    args=[recording_id, video_output, videos, subtime, presenter],
106
                )
NEW
107
                t.daemon = True
×
NEW
108
                t.start()
×
109
        else:
NEW
110
            encode_video_studio(recording_id, video_output, videos, subtime, presenter)
×
111

112

113
def encode_video_studio(recording_id, video_output, videos, subtime, presenter) -> None:
1✔
114
    """ENCODE STUDIO: MAIN FUNCTION."""
115
    msg = ""
×
116
    if USE_REMOTE_ENCODING_TRANSCODING:
×
117
        start_studio_task.delay(recording_id, video_output, videos, subtime, presenter)
×
118
    else:
119
        msg = start_encode_video_studio(video_output, videos, subtime, presenter)
×
120
        store_encoding_studio_info(recording_id, video_output, msg)
×
121

122

123
def store_encoding_studio_info(recording_id, video_output, msg) -> None:
1✔
124
    recording = Recording.objects.get(id=recording_id)
×
125
    recording.comment += msg
×
126
    recording.save()
×
127
    if check_file(video_output):
×
128
        from pod.recorder.plugins.type_studio import save_basic_video
×
129

130
        video = save_basic_video(recording, video_output)
×
131
        encode_video(video.id)
×
132
    else:
133
        msg = "Wrong file or path:\n%s" % video_output
×
134
        send_email_recording(msg, recording_id)
×
135

136

137
def encode_video(video_id: int) -> None:
1✔
138
    """ENCODE VIDEO: MAIN FUNCTION."""
139
    start = "Start at: %s" % time.ctime()
1✔
140

141
    video_to_encode = Video.objects.get(id=video_id)
1✔
142
    video_to_encode.encoding_in_progress = True
1✔
143
    video_to_encode.save()
1✔
144

145
    if not check_file(video_to_encode.video.path):
1✔
146
        msg = "Wrong file or path:\n%s" % video_to_encode.video.path
1✔
147
        add_encoding_log(video_id, msg)
1✔
148
        change_encoding_step(video_id, -1, msg)
1✔
149
        send_email(msg, video_id)
1✔
150
        return
1✔
151

152
    change_encoding_step(video_id, 0, "start")
1✔
153
    # start and stop cut?
154
    encoding_video = get_encoding_video(video_to_encode)
1✔
155
    encoding_video.add_encoding_log("start_time", "", True, start)
1✔
156
    change_encoding_step(video_id, 1, "remove old data")
1✔
157
    encoding_video.remove_old_data()
1✔
158

159
    if USE_REMOTE_ENCODING_TRANSCODING:
1✔
160
        change_encoding_step(video_id, 2, "start remote encoding")
1✔
161
        dressing = None
1✔
162
        dressing_input = ""
1✔
163
        if Dressing.objects.filter(videos=video_to_encode).exists():
1✔
164
            dressing = Dressing.objects.get(videos=video_to_encode)
1✔
165
            if dressing:
1✔
166
                dressing_input = get_dressing_input(dressing, FFMPEG_DRESSING_INPUT)
1✔
167
        start_encoding_task.delay(
1✔
168
            encoding_video.id,
169
            encoding_video.video_file,
170
            encoding_video.cutting_start,
171
            encoding_video.cutting_stop,
172
            json_dressing=dressing.to_json() if dressing else None,
173
            dressing_input=dressing_input,
174
        )
175
    else:
176
        change_encoding_step(video_id, 2, "start standard encoding")
1✔
177
        encoding_video.start_encode()
1✔
178
        final_video = store_encoding_info(video_id, encoding_video)
1✔
179

180
        if encoding_video.error_encoding:
1✔
181
            enc_log, created = EncodingLog.objects.get_or_create(video=final_video)
×
182
            msg = "Error during video `%s` encoding." % video_id
×
183
            if created is False:
×
184
                msg += " See log at:\n%s" % enc_log.logfile.url
×
185
            change_encoding_step(video_id, -1, msg)
×
186
            send_email(msg, video_id)
×
187
        else:
188
            end_of_encoding(final_video)
1✔
189

190

191
def store_encoding_info(video_id: int, encoding_video: Encoding_video_model) -> Video:
1✔
192
    """Store all encoding file and informations from encoding tasks."""
193
    change_encoding_step(video_id, 3, "store encoding info")
1✔
194
    final_video = encoding_video.store_json_info()
1✔
195
    final_video.is_video = final_video.get_video_m4a() is None
1✔
196
    final_video.encoding_in_progress = False
1✔
197
    final_video.save()
1✔
198
    return final_video
1✔
199

200

201
def get_encoding_video(video_to_encode: Video) -> Encoding_video_model:
1✔
202
    """Get the encoding video object from video."""
203
    dressing = None
1✔
204
    dressing_input = ""
1✔
205
    if Dressing.objects.filter(videos=video_to_encode).exists():
1✔
206
        dressing = Dressing.objects.get(videos=video_to_encode)
1✔
207
        if dressing:
1✔
208
            dressing_input = get_dressing_input(dressing, FFMPEG_DRESSING_INPUT)
1✔
209

210
    if CutVideo.objects.filter(video=video_to_encode).exists():
1✔
211
        cut = CutVideo.objects.get(video=video_to_encode)
1✔
212
        cut_start = time_to_seconds(cut.start)
1✔
213
        cut_end = time_to_seconds(cut.end)
1✔
214
        encoding_video = Encoding_video_model(
1✔
215
            video_to_encode.id,
216
            video_to_encode.video.path,
217
            cut_start,
218
            cut_end,
219
            json_dressing=dressing.to_json() if dressing else None,
220
            dressing_input=dressing_input,
221
        )
222
        return encoding_video
1✔
223

224
    return Encoding_video_model(
1✔
225
        video_to_encode.id,
226
        video_to_encode.video.path,
227
        0,
228
        0,
229
        json_dressing=dressing.to_json() if dressing else None,
230
        dressing_input=dressing_input,
231
    )
232

233

234
def end_of_encoding(video: Video) -> None:
1✔
235
    """Notify user at the end of encoding & call transcription."""
236
    if (
1✔
237
        USE_NOTIFICATIONS
238
        and video.owner.owner.accepts_notifications
239
        and PushInformation.objects.filter(user=video.owner).exists()
240
    ):
241
        send_notification_encoding(video)
×
242
    if EMAIL_ON_ENCODING_COMPLETION:
1✔
243
        send_email_encoding(video)
1✔
244

245
    transcript_video(video.id)
1✔
246
    change_encoding_step(video.id, 0, "end of encoding")
1✔
247

248

249
def transcript_video(video_id: int) -> None:
1✔
250
    """Transcript video audio to text."""
251
    video = Video.objects.get(id=video_id)
1✔
252
    if USE_TRANSCRIPTION and video.transcript not in ["", "0", "1"]:
1✔
253
        change_encoding_step(video_id, 4, "transcript video")
×
254
        start_transcript_video = getattr(transcript, TRANSCRIPT_VIDEO)
×
255
        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

© 2026 Coveralls, Inc