Skip to content

Commit

Permalink
feat(transcripts): #230 Use duration from file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
allishultes committed May 14, 2021
1 parent fd55554 commit 9b7b2d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Components/Workspace/Transcripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { withAuthorization } from '../../Session';
import TranscriptRow from '@bbc/digital-paper-edit-storybook/TranscriptRow';
import { formatDates } from '../../../Util/time';
import './index.scss';

const Transcripts = (props) => {
const items = props.items;
Expand All @@ -23,7 +24,7 @@ const Transcripts = (props) => {
created={ created ? created : 'NA' }
updated={ updated ? updated : 'NA' }
message={ item.message }
mediaDuration={ item.runtime?.humanReadable ? item.runtime?.humanReadable : item.duration }
mediaDuration={ item.duration ? item.duration : null }
transcriptionDuration={ item.transcriptionDuration }
status={ item.status }
progress={ progress }
Expand Down
27 changes: 20 additions & 7 deletions src/Components/Workspace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,29 @@ const WorkspaceView = props => {

// general

const finishCreateorUpdateTranscript = async (transcript, duration) => {
const formatDuration = async (duration) => {
const seconds = Number(duration);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.floor(seconds % 60);

const mDisplay = m > 0 ? m + (m === 1 ? ' min ' : ' mins ') : '';
const sDisplay = s > 0 ? s + (s === 1 ? ' s' : ' s') : '';

return mDisplay + sDisplay;
};

const finishCreateOrUpdateTranscript = async (transcript, duration, video) => {
video.remove();
const file = transcript.file;
delete transcript.file;
const formattedDuration = `${ parseInt(duration / 60, 10) } mins`;

const newTranscript = await createOrUpdateCollectionItem({
...transcript,
title: transcript.title,
projectId: id,
description: transcript.description ? transcript.description : '',
status: 'uploading',
duration: formattedDuration
duration: duration,
}, createTranscript, updateTranscript);

asyncUploadFile(newTranscript.id, file);
Expand All @@ -305,17 +316,18 @@ const WorkspaceView = props => {

};

const getDuration = async (newTranscript) => {
const createOrUpdateWithDuration = async (newTranscript) => {
const file = newTranscript.file;
const video = document.createElement('video');
video.setAttribute('id', 'preload');
video.preload = 'metadata';

// eslint-disable-next-line no-return-assign
video.onloadedmetadata = async () => {
window.URL.revokeObjectURL(video.src);
const duration = video.duration;
const formattedDuration = await formatDuration(duration);

return await finishCreateorUpdateTranscript(newTranscript, duration);
return await finishCreateOrUpdateTranscript(newTranscript, formattedDuration, video);
};

video.src = URL.createObjectURL(file);
Expand All @@ -332,6 +344,7 @@ const WorkspaceView = props => {
};

const createOrUpdateTranscript = async (item) => {
console.log('trying to create a transcript...', item);
let newTranscript = { ...item, projectId: id };
delete newTranscript.display;

Expand All @@ -343,7 +356,7 @@ const WorkspaceView = props => {
return newTranscript;

} else {
return await getDuration(newTranscript);
return await createOrUpdateWithDuration(newTranscript);
}
};

Expand Down

0 comments on commit 9b7b2d2

Please sign in to comment.