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

EsupPortail / Esup-Pod / 5423251035

pending completion
5423251035

Pull #899

github

web-flow
Merge 01a450543 into b8d45a116
Pull Request #899: [WIP] Ptitloup/feature new encoding

335 of 335 new or added lines in 8 files covered. (100.0%)

9029 of 12629 relevant lines covered (71.49%)

0.71 hits per line

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

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

3
from django.conf import settings
1✔
4
from pod.video.models import Video
1✔
5
from .Encoding_video_model import Encoding_video_model
1✔
6
from .encoding_studio import encode_video_studio
1✔
7

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

22
__license__ = "LGPL v3"
1✔
23
log = logging.getLogger(__name__)
1✔
24

25
USE_TRANSCRIPTION = getattr(settings, "USE_TRANSCRIPTION", False)
1✔
26

27
if USE_TRANSCRIPTION:
1✔
28
    from . import transcript
×
29
    TRANSCRIPT_VIDEO = getattr(settings, "TRANSCRIPT_VIDEO", "start_transcript")
×
30

31
CELERY_TO_ENCODE = getattr(settings, "CELERY_TO_ENCODE", False)
1✔
32
EMAIL_ON_ENCODING_COMPLETION = getattr(settings, "EMAIL_ON_ENCODING_COMPLETION", True)
1✔
33

34
USE_DISTANT_ENCODING_TRANSCODING = getattr(
1✔
35
    settings,
36
    "USE_DISTANT_ENCODING_TRANSCODING",
37
    False
38
)
39
if USE_DISTANT_ENCODING_TRANSCODING:
1✔
40
    from .encoding_tasks import start_encoding_task
×
41

42
# ##########################################################################
43
# ENCODE VIDEO: THREAD TO LAUNCH ENCODE
44
# ##########################################################################
45

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

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

58

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

69

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

85

86
"""
87
def start_studio_remote_encode(recording_id, video_output, videos, subtime, presenter):
88
    # load module here to prevent circular import
89
    from .remote_encode import remote_encode_studio
90

91
    log.info("START ENCODE RECORDING ID %s" % recording_id)
92
    t = threading.Thread(
93
        target=remote_encode_studio,
94
        args=[recording_id, video_output, videos, subtime, presenter],
95
    )
96
    t.setDaemon(True)
97
    t.start()
98
"""
99

100

101
def encode_video(video_id):
1✔
102
    """ENCODE VIDEO: MAIN FUNCTION."""
103
    start = "Start at: %s" % time.ctime()
1✔
104

105
    video_to_encode = Video.objects.get(id=video_id)
1✔
106
    video_to_encode.encoding_in_progress = True
1✔
107
    video_to_encode.save()
1✔
108

109
    if not check_file(video_to_encode.video.path):
1✔
110
        msg = "Wrong file or path:" + "\n%s" % video_to_encode.video.path
1✔
111
        add_encoding_log(video_id, msg)
1✔
112
        change_encoding_step(video_id, -1, msg)
1✔
113
        send_email(msg, video_id)
1✔
114
        return
1✔
115

116
    change_encoding_step(video_id, 0, "start")
1✔
117
    # start and stop cut ?
118
    encoding_video = get_encoding_video(video_to_encode)
1✔
119
    encoding_video.add_encoding_log("start_time", "", True, start)
1✔
120
    change_encoding_step(video_id, 1, "remove old data")
1✔
121
    encoding_video.remove_old_data()
1✔
122

123
    change_encoding_step(video_id, 2, "start encoding")
1✔
124
    if USE_DISTANT_ENCODING_TRANSCODING:
1✔
125
        start_encoding_task.delay(
×
126
            encoding_video.id,
127
            encoding_video.video_file,
128
            encoding_video.cutting_start,
129
            encoding_video.cutting_stop
130
        )
131
    else:
132
        encoding_video.start_encode()
1✔
133
        final_video = store_encoding_info(video_id, encoding_video)
1✔
134
        end_of_encoding(final_video)
1✔
135

136

137
def store_encoding_info(video_id, encoding_video):
1✔
138
    change_encoding_step(video_id, 3, "store encoding info")
1✔
139
    final_video = encoding_video.store_json_info()
1✔
140
    final_video.is_video = final_video.get_video_m4a() is None
1✔
141
    final_video.encoding_in_progress = False
1✔
142
    final_video.save()
1✔
143
    return final_video
1✔
144

145

146
def get_encoding_video(video_to_encode):
1✔
147
    if CutVideo.objects.filter(video=video_to_encode).exists():
1✔
148
        cut = CutVideo.objects.get(video=video_to_encode)
×
149
        cut_start = time_to_seconds(cut.start)
×
150
        cut_end = time_to_seconds(cut.end)
×
151
        encoding_video = Encoding_video_model(
×
152
            video_to_encode.id, video_to_encode.video.path, cut_start, cut_end
153
        )
154
        return encoding_video
×
155
    return Encoding_video_model(video_to_encode.id, video_to_encode.video.path)
1✔
156

157

158
def end_of_encoding(video):
1✔
159
    # envois mail fin encodage
160
    if EMAIL_ON_ENCODING_COMPLETION:
1✔
161
        send_email_encoding(video)
1✔
162

163
    transcript_video(video.id)
1✔
164
    change_encoding_step(video.id, 0, "end of encoding")
1✔
165

166

167
def transcript_video(video_id):
1✔
168
    """Transcript video audio to text."""
169
    video = Video.objects.get(id=video_id)
1✔
170
    if USE_TRANSCRIPTION and video.transcript not in ["", "0", "1"]:
1✔
171
        change_encoding_step(video_id, 4, "transcript video")
×
172
        start_transcript_video = getattr(transcript, TRANSCRIPT_VIDEO)
×
173
        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