Skip to content

Commit

Permalink
Merge pull request #32 from nattvara/update-capture-preview
Browse files Browse the repository at this point in the history
Update capture preview job
  • Loading branch information
nattvara authored Mar 12, 2023
2 parents e828b37 + 5455ac9 commit 188592c
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 99 deletions.
20 changes: 11 additions & 9 deletions jobs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,17 @@ def schedule_analysis_of_lecture(

save_message_for_analysis(analysis, 'Analysis scheduled', 'Waiting for a worker to pick it up.')

next(get_metadata_queue()).enqueue(capture_preview.job, lecture.public_id, lecture.language, job_timeout=capture_preview.TIMEOUT) # noqa: E501
next(get_metadata_queue()).enqueue(fetch_metadata.job, lecture.public_id, lecture.language, job_timeout=fetch_metadata.TIMEOUT) # noqa: E501
job_1 = next(queue_download()).enqueue(download_lecture.job, lecture.public_id, lecture.language, job_timeout=download_lecture.TIMEOUT) # noqa: E501
job_2 = next(queue_extract()).enqueue(extract_audio.job, lecture.public_id, lecture.language, job_timeout=extract_audio.TIMEOUT, depends_on=job_1) # noqa: E501
job_3 = next(queue_transcribe()).enqueue(transcribe_audio.job, lecture.public_id, lecture.language, job_timeout=transcribe_audio.TIMEOUT, depends_on=job_2) # noqa: E501
job_4 = next(queue_summarise()).enqueue(summarise_transcript.job, lecture.public_id, lecture.language, job_timeout=summarise_transcript.TIMEOUT, depends_on=job_3) # noqa: E501
job_5 = next(queue_metadata()).enqueue(clean_lecture.job, lecture.public_id, lecture.language, depends_on=job_4) # noqa: F841, E501
job_6 = next(queue_metadata()).enqueue(capture_preview.job, lecture.public_id, lecture.language, job_timeout=capture_preview.TIMEOUT, depends_on=job_4) # noqa: E501
job_7 = next(queue_metadata()).enqueue(index_lecture.job, lecture.public_id, lecture.language, depends_on=job_6) # noqa: F841, E501
# analysis sequence
download = next(queue_download()).enqueue(download_lecture.job, lecture.public_id, lecture.language, job_timeout=download_lecture.TIMEOUT) # noqa: E501
extract = next(queue_extract()).enqueue(extract_audio.job, lecture.public_id, lecture.language, job_timeout=extract_audio.TIMEOUT, depends_on=download) # noqa: E501
transcribe = next(queue_transcribe()).enqueue(transcribe_audio.job, lecture.public_id, lecture.language, job_timeout=transcribe_audio.TIMEOUT, depends_on=extract) # noqa: E501
summarise = next(queue_summarise()).enqueue(summarise_transcript.job, lecture.public_id, lecture.language, job_timeout=summarise_transcript.TIMEOUT, depends_on=transcribe) # noqa: E501

# other jobs that depend on different steps in the analysis
metadata = next(get_metadata_queue()).enqueue(fetch_metadata.job, lecture.public_id, lecture.language, job_timeout=fetch_metadata.TIMEOUT) # noqa: F841, E501
preview = next(queue_metadata()).enqueue(capture_preview.job, lecture.public_id, lecture.language, job_timeout=capture_preview.TIMEOUT, depends_on=download) # noqa: F841, E501
clean = next(queue_metadata()).enqueue(clean_lecture.job, lecture.public_id, lecture.language, depends_on=summarise) # noqa: F841, E501
index = next(queue_metadata()).enqueue(index_lecture.job, lecture.public_id, lecture.language, depends_on=summarise) # noqa: F841, E501

return analysis

Expand Down
32 changes: 6 additions & 26 deletions jobs/capture_preview.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,25 @@
from config.settings import settings
from redis import Redis
from rq import Queue
import logging

from db.crud import get_lecture_by_public_id_and_language
from tools.video.img import save_photo_from_video
from tools.web.camera import save_photo
from db.models import Lecture
import jobs.capture_preview
import tools.video.img

# 5min timeout
TIMEOUT = 5 * 60


def job(lecture_id: str, language: str):
logger = logging.getLogger('rq.worker')

lecture = get_lecture_by_public_id_and_language(lecture_id, language)
if lecture.raw_content_link is not None:
logger.info('content has raw link, using mp4 file')
if lecture.mp4_filepath is None:
raise ValueError('mp4_filepath was not specified')

mp4 = lecture.mp4_filepath
save_photo_from_video(mp4, lecture.preview_filename())
save_photo_from_video(mp4, lecture.preview_small_filename(), True)

lecture.refresh()
lecture.img_preview = lecture.preview_filename()
lecture.img_preview_small = lecture.preview_small_filename()
lecture.save()
return

url = lecture.content_link()
logger.info(f'taking photo of {url}')

save_photo(url, lecture)
logger.info(f'photo saved at {lecture.preview_filename()}')
if lecture.mp4_filepath is None:
raise ValueError('mp4_filepath was not specified')

save_photo(url, lecture, small=True)
logger.info(f'photo saved at {lecture.preview_small_filename()}')
mp4 = lecture.mp4_filepath
tools.video.img.save_photo_from_video(mp4, lecture.preview_filename())
tools.video.img.save_photo_from_video(mp4, lecture.preview_small_filename(), True)

lecture.refresh()
lecture.img_preview = lecture.preview_filename()
Expand Down
10 changes: 0 additions & 10 deletions jobs/download_lecture.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,6 @@ def job(lecture_id: str, language: str):
download_mp4_from_youtube(lecture)
elif lecture.source == Lecture.Source.KTH_RAW:
download_mp4_from_m3u8(lecture.raw_content_link, lecture)

logger.info('rq-queueing preview capture job')
from jobs import get_metadata_queue
q = next(get_metadata_queue())
q.enqueue(
jobs.capture_preview.job,
lecture.public_id,
lecture.language,
job_timeout=jobs.capture_preview.TIMEOUT
)
else:
raise ValueError(f'unsupported lecture source {lecture.source}')

Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,8 @@ def mp4_file():
@pytest.fixture
def mp3_file():
return os.path.join(os.path.dirname(__file__), 'files') + '/example.mp3'


@pytest.fixture
def img_file():
return os.path.join(os.path.dirname(__file__), 'files') + '/example.png'
27 changes: 27 additions & 0 deletions tests/feature/jobs/test_capture_preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import shutil

from jobs import capture_preview
from db.models import Lecture


def test_download_of_kth_play_lecture_saves_mp4_file(mocker, mp4_file, img_file):
def save_photo_from_video(mp4_filepath: str, output_file: str, small=False):
shutil.copy(img_file, output_file)

func = mocker.patch('tools.video.img.save_photo_from_video', side_effect=save_photo_from_video)

lecture = Lecture(
public_id='some_id',
language=Lecture.Language.SWEDISH,
mp4_filepath=mp4_file,
approved=True,
source=Lecture.Source.KTH,
)
lecture.save()

capture_preview.job(lecture.public_id, lecture.language)
lecture.refresh()

assert lecture.img_preview == lecture.preview_filename()
assert lecture.img_preview_small == lecture.preview_small_filename()
assert func.call_count == 2
Binary file added tests/files/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 0 additions & 54 deletions tools/web/camera.py

This file was deleted.

4 changes: 4 additions & 0 deletions web-ui/src/components/preview.less
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
color: black;
}

.source {
color: black;
}

.length {
color: black;
}
Expand Down
23 changes: 23 additions & 0 deletions web-ui/src/components/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export function PreviewCompact(props: PreviewCompactProps) {
icon = youtubeLogoSmall;
} else if (lecture.source === 'kth') {
icon = kthLogo;
} else if (lecture.source === 'kth_raw') {
icon = kthLogo;
}

return (
Expand Down Expand Up @@ -134,6 +136,15 @@ export default function Preview(props: PreviewProps) {
flagIcon = enFlag;
}

let icon = '';
if (lecture.source === 'youtube') {
icon = youtubeLogoSmall;
} else if (lecture.source === 'kth') {
icon = kthLogo;
} else if (lecture.source === 'kth_raw') {
icon = kthLogo;
}

let dateString = '';
if (lecture.date !== null) {
dateString = new Date(lecture.date).toISOString().split('T')[0];
Expand Down Expand Up @@ -186,6 +197,18 @@ export default function Preview(props: PreviewProps) {
preview={false}
/>
</Col>
<Col>
<h3 className={styles.source}>From</h3>
</Col>
<Col>
<Image
src={icon}
height={20}
width={20}
className={styles.logo}
preview={false}
/>
</Col>
</Space>
</Row>
</div>
Expand Down

0 comments on commit 188592c

Please sign in to comment.