Skip to content

Commit 1b08a94

Browse files
authored
Merge pull request #434 from mayeaux/adding-sprite-images
Adding sprite images
2 parents 1b9c1de + bfc29fd commit 1b08a94

File tree

6 files changed

+255
-5
lines changed

6 files changed

+255
-5
lines changed

controllers/backend/uploading.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const sendMessageToDiscord = require('../../lib/moderation/discordWebhooks');
2929
const { saveAndServeFilesDirectory } = require('../../lib/helpers/settings');
3030
const getMediaType = require('../../lib/uploading/media');
3131
const { b2, bucket, hostUrl } = require('../../lib/uploading/backblaze');
32+
const createSpriteImageAndVtt = require('../../lib/uploading/createSpriteImages');
3233

3334
const ffmpegHelper = require('../../lib/uploading/ffmpeg');
3435
const {
@@ -424,6 +425,7 @@ exports.postFileUpload = async(req, res) => {
424425

425426
let bitrate, codecName, codecProfile;
426427

428+
// if not an image, requires ffprobe data collection
427429
if(upload.fileType !== 'image'){
428430

429431
// load the ffprobe data in response
@@ -499,12 +501,16 @@ exports.postFileUpload = async(req, res) => {
499501

500502
console.log('done moving file');
501503

504+
505+
/** BASIC VIDEO PROCESSING COMPLETED, CHECKING IF NEEDS CONVERSION **/
506+
502507
const specificMatches = ( codecName == 'hevc' || codecProfile == 'High 4:4:4 Predictive' );
503508

504509
if(specificMatches || bitrate > maxBitrate){
505510
upload.fileType = 'convert';
506511
}
507512

513+
// TODO: a bit ugly, wish we could pull out 'only video' stuff and then do convert in its own conditional
508514
/** TELL THE USER WE ARE CONVERTING / COMPRESSING THEIR VIDEO **/
509515
if(upload.fileType == 'convert' || bitrate > maxBitrate || upload.fileType == 'video'){
510516

@@ -516,6 +522,7 @@ exports.postFileUpload = async(req, res) => {
516522
// set upload progress as 1 so it has something to show on the frontend
517523
redisClient.setAsync(`${uniqueTag}uploadProgress`, 'Your upload is beginning processing...');
518524

525+
// video is marked as processing, end the request and send user to upload page
519526
if(!responseSent){
520527
responseSent = true;
521528
aboutToProcess(res, channelUrl, uniqueTag);
@@ -532,13 +539,14 @@ exports.postFileUpload = async(req, res) => {
532539

533540
// TODO: savePath and fileInDirectory are the same thing, need to clean this code up
534541

542+
// if we need to convert, move it to this 'old' location so we can write to the new location
535543
if(fileExtension == '.mp4' && bitrate > maxBitrate || specificMatches){
536544
await fs.move(savePath, `${saveAndServeFilesDirectory}/${channelUrl}/${uniqueTag}-old.mp4`);
537545

538546
fileInDirectory = `${saveAndServeFilesDirectory}/${channelUrl}/${uniqueTag}-old.mp4`;
539547
}
540548

541-
// if the file type is convert or it's over max bitrate
549+
// if the file type is convert or it's over max bitrate, convert it with ffmpeg
542550
if(upload.fileType == 'convert' || (bitrate > maxBitrate && fileExtension == '.mp4')){
543551

544552
console.log(fileInDirectory);
@@ -553,7 +561,8 @@ exports.postFileUpload = async(req, res) => {
553561

554562
uploadLogger.info('Finished converting file', logObject);
555563

556-
// assuming an mp4 is created at this point so we delete the old uncoverted video
564+
// assuming an mp4 is created at this point so we delete the old unconverted video
565+
// note, $fileInDirectory is changed
557566
await fs.remove(`${fileInDirectory}`);
558567

559568
uploadLogger.info('Deleted unconverted file', logObject);
@@ -625,6 +634,19 @@ exports.postFileUpload = async(req, res) => {
625634
responseSent = true;
626635
aboutToProcess(res, channelUrl, uniqueTag);
627636
}
637+
638+
// once everything is done, generate thumbnail previews
639+
if(upload.fileType === 'video'){
640+
// will automatically stick it at /uploads/$channelUrl/$uniqueTag_sprite.png and /$uniqueTag_sprite.vtt
641+
(async function(){
642+
if(user.plan === 'plus') {
643+
await createSpriteImageAndVtt(channelUrl, uniqueTag, fileInDirectory)
644+
645+
upload.hasPreviewSpriteThumbnail = true;
646+
await upload.save();
647+
}
648+
})()
649+
}
628650
});
629651

630652
// });
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const createSpriteWithVTT = require('generate-video-sprites-node');
2+
/** input and output paths **/
3+
4+
/** variables to setup the output of the sprite/vtt **/
5+
// how often should a snapshot be taken
6+
const intervalInSecondsAsInteger = 1;
7+
8+
// size of the hover image
9+
const widthInPixels = 300;
10+
const heightInPixels = 169;
11+
12+
// how many columns to use, seems arbitrary so I'll use 5
13+
const columns = 5;
14+
15+
async function createWebVttAndSpriteImage(channelUrl, uploadUniqueTag, uploadFilePath){
16+
17+
/** input and output paths **/
18+
// used in the paths, could use different names if you want
19+
const filename = uploadUniqueTag
20+
21+
// the file to create sprite and vtt from
22+
const inputFile = uploadFilePath;
23+
24+
const spriteFileName = `${uploadUniqueTag}_sprite.png`;
25+
const vttFileName = `${uploadUniqueTag}_sprite.vtt`;
26+
27+
const prependPath = `/uploads/${channelUrl}`
28+
29+
// where to output the files
30+
const spriteOutputFilePath = `./uploads/${channelUrl}/${spriteFileName}`;
31+
const webVTTOutputFilePath = `./uploads/${channelUrl}/${vttFileName}`;
32+
33+
const pathToGenerator = './node_modules/generate-video-sprites-node/generator'
34+
35+
await createSpriteWithVTT({
36+
pathToGenerator,
37+
inputFile,
38+
intervalInSecondsAsInteger,
39+
widthInPixels, heightInPixels, columns,
40+
spriteOutputFilePath,
41+
webVTTOutputFilePath,
42+
prependPath,
43+
filename,
44+
spriteFileName
45+
})
46+
47+
console.log('DONE CREATING SPRITE IMAGE AND WEBVTT!');
48+
}
49+
50+
module.exports = createWebVttAndSpriteImage

package-lock.json

Lines changed: 177 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"forever": "^2.0.0",
6767
"formidable": "^1.2.1",
6868
"fs-extra": "^4.0.3",
69+
"generate-video-sprites-node": "^1.0.3",
6970
"get-audio-duration": "^2.0.0",
7071
"get-video-duration": "^1.0.3",
7172
"heroku-ssl-redirect": "0.0.4",

views/mediaPlayerPartials/plyrAndAutoplayFunctionalityJs.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ script.
113113
seekTime,
114114
};
115115

116-
const hasPreviewThumbnail = #{upload.hasPreviewSpriteThumbnail}
116+
const hasPreviewThumbnail = '#{upload.hasPreviewSpriteThumbnail}'
117117

118-
if(hasPreviewThumbnail){
118+
if(hasPreviewThumbnail === 'true'){
119119
const previewThumbnails = {
120120
enabled: true,
121121
src: [`/uploads/#{upload.uploader.channelUrl}/#{upload.uniqueTag}_sprite.vtt`],

views/widgets/zopim.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ script.
55
setTimeout(function(){
66
// TODO: thing here
77
$zopim(function () {
8-
$zopim.livechat.window.hide();
8+
// $zopim.livechat.window.hide();
99
});
1010
}, 2000)
1111
//End of Zendesk Widget script

0 commit comments

Comments
 (0)