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

EsupPortail / Esup-Pod / 5462629459

pending completion
5462629459

Pull #899

github

web-flow
Merge e759dacb6 into c94f0e331
Pull Request #899: [DONE] Ptitloup/feature new encoding

400 of 400 new or added lines in 14 files covered. (100.0%)

8968 of 12675 relevant lines covered (70.75%)

0.71 hits per line

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

76.47
/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, threaded=True):
1✔
60
    """Start local encoding."""
61
    if threaded:
1✔
62
        if CELERY_TO_ENCODE:
1✔
63
            task_start_encode.delay(video_id)
×
64
        else:
65
            log.info("START ENCODE VIDEO ID %s" % video_id)
1✔
66
            t = threading.Thread(target=encode_video, args=[video_id])
1✔
67
            t.setDaemon(True)
1✔
68
            t.start()
1✔
69
    else:
70
        encode_video(video_id)
×
71

72

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

88

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

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

103

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

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

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

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

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

139

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

149

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

162

163
def end_of_encoding(video):
1✔
164
    """Send mail at the end of encoding, call transcription."""
165
    if EMAIL_ON_ENCODING_COMPLETION:
1✔
166
        send_email_encoding(video)
1✔
167

168
    transcript_video(video.id)
1✔
169
    change_encoding_step(video.id, 0, "end of encoding")
1✔
170

171

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