Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
charleprr committed May 24, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1d94a48 commit 18b1ee2
Showing 10 changed files with 303 additions and 156 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Temporary resources
temporary/*
tmp/*

# Logs
logs
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const Redditube = require(`./modules/Redditube.js`);
Redditube.make(`r/askreddit`, 3, `hot`, `all`);

(async () => {
await Redditube.make(`r/askreddit`, 3, `hot`, `all`);
// await Redditube.upload();
})();
4 changes: 2 additions & 2 deletions modules/Image.js
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ function wrapText(context, text, x, y, maxWidth, lineHeight) {
}

function generatePostImage(post) {
let filepath = `./temporary/${post.id}.png`;
let filepath = `./tmp/${post.id}.png`;
console.log(`Generating ${filepath}`);

const canvas = createCanvas(1920, 1080);
@@ -74,7 +74,7 @@ function generatePostImage(post) {
}

function generateCommentImage(comment) {
let filepath = `./temporary/${comment.id}.png`;
let filepath = `./tmp/${comment.id}.png`;
console.log(`Generating ${filepath}`);

const canvas = createCanvas(1920, 1080);
12 changes: 4 additions & 8 deletions modules/Redditube.js
Original file line number Diff line number Diff line change
@@ -10,27 +10,23 @@
const Reddit = require(`./Reddit.js`);
const Image = require(`./Image.js`);
const Sound = require(`./Sound.js`);
const Video = require(`./Video2.js`);
const Video = require(`./Video.js`);
const YouTube = require(`./YouTube.js`);

module.exports = {

make: (subreddit, count, sort=`top`, time=`all`) => {
return new Promise(async resolve => {

return new Promise(async resolve => {
let post = await Reddit.fetchPost(subreddit, sort, time);
let comments = await Reddit.fetchComments(subreddit, post.id, count);

//await Image.generate(post, comments);
//await Sound.generate(post, comments);
await Video.generate(post, comments);

resolve();
});
},

upload: async (title, description) => {
YouTube.upload();
upload: (title, description, tags, privacyStatus) => {
YouTube.upload(title, description, tags, privacyStatus);
}

};
2 changes: 1 addition & 1 deletion modules/Sound.js
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ const util = require(`util`);
const client = new textToSpeech.TextToSpeechClient();

function TTS(text, id) {
let filepath = `./temporary/${id}.mp3`;
let filepath = `./tmp/${id}.mp3`;
console.log(`Generating ${filepath}`);
return new Promise(async resolve => {
const request = {
103 changes: 55 additions & 48 deletions modules/Video.js
Original file line number Diff line number Diff line change
@@ -7,70 +7,77 @@
*
* @copyright (C) 2020 by Charly Poirier
*/
const videoStitch = require(`video-stitch`).concat;
const mp3Duration = require(`mp3-duration`);
const videoShow = require(`videoshow`);
const configuration = {
fps: 24,
loop: 7,
transition: false,
videoBitrate: 1024,
size: `1920x1080`,
format: `mp4`,
videoCodec: `libx264`,
outputOptions: [`-pix_fmt yuv420p`]
}
const ffmpeg = require(`fluent-ffmpeg`);

let generateClip = async (id) => {
let image = `./temporary/${id}.png`;
let voice = `./temporary/${id}.mp3`;
let output = `./temporary/${id}.mp4`;
console.log(`Generating ${output}`);
let duration = await mp3Duration(voice);
configuration.loop = duration;
return new Promise((resolve, reject) => {
videoShow([image], configuration).audio(voice).save(output)
.on(`error`, (error, stdout, stderr) => {
console.log(error);
console.log(stdout);
console.log(stderr);
reject();
}).on(`end`, () => {
resolve();
});
console.log(`Generating ./tmp/${id}.mp4`);

let clip = new ffmpeg()
// Image
.addInput(`./tmp/${id}.png`)
.loop()
// Audio
.addInput(`./tmp/${id}.mp3`)
.addOption(`-shortest`)
.audioCodec(`libmp3lame`)
.audioBitrate(128)
// Configuration
.size(`1280x720`)
.format(`mp4`)
.fps(30)
.videoCodec(`libx264`)
.videoBitrate(5000)
.addOption(`-pix_fmt yuv420p`);

return new Promise(resolve => {
clip.save(`./tmp/${id}.mp4`).on(`end`, resolve);
});
}

let mergeClips = (post, comments) => {
let clips = [{"fileName":`../home/pi/projects/redditube/temporary/${post.id}.mp4`}];
comments.forEach(comment => clips.push({"fileName":`../home/pi/projects/redditube/temporary/${comment.id}.mp4`}));
console.log(`\nMerging clips`);
let mergeClips = () => {
console.log(`Merging clips`);

let video = new ffmpeg();
// Post
video.addInput(`./tmp/${post.id}.mp4`);
video.addInput(`./resources/glitch.mp4`);
// Comments
for (let comment of comments) {
video.addInput(`./tmp/${comment.id}.mp4`);
video.addInput(`./resources/glitch.mp4`);
}

return new Promise(resolve => {
videoStitch({silent: true, overwrite: true})
.clips(clips)
.output(`./final_video.mp4`)
.concat()
.then((filename) => {
console.log(`Done! Check ${filename}.`);
resolve();
});
video.mergeToFile(`./tpm/video.mp4`, `./tmp/`).on(`end`, () => resolve());
});
}

let addBackgroundMusic = () => {
console.log(`Adding background music`);

let video = new ffmpeg()
.addInput(`./tpm/video.mp4`)
.addInput(`./resources/lofi.mp3`)
.addOptions([
`-filter_complex [0:a]aformat=fltp:44100:stereo,apad[0a];[1]aformat=fltp:44100:stereo,volume=0.3[1a];[0a][1a]amerge[a]`,
`-map 0:v -map [a] -ac 2`,
`-shortest`
]);

return new Promise(resolve => {
video.save(`video.mp4`).on('end', () => resolve());
});
}

module.exports = {
generate: (post, comments) => {
return new Promise(async resolve => {

// Generate clips

await generateClip(post.id);
for (let comment of comments) await generateClip(comment.id);

// Merge clips
await mergeClips(post, comments);

// Transitions?

// Background music?
await addBackgroundMusic(post, comments);

resolve();
});
74 changes: 0 additions & 74 deletions modules/Video2.js

This file was deleted.

3 changes: 3 additions & 0 deletions modules/YouTube.js
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@
* @copyright (C) 2020 by Charly Poirier
*/

// YouTube API: AIzaSyAFfTodx67vV7UHkfiqSv0xqv4Odr5Lw20
// Video category: 24 (entertainment)

module.exports = {
upload: async () => {
console.error(`Not supported yet.`);
251 changes: 230 additions & 21 deletions package-lock.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
"@google-cloud/text-to-speech": "^2.3.0",
"canvas": "^2.6.1",
"get-json": "^1.0.1",
"google-auth-library": "^6.0.0",
"googleapis": "^51.0.0",
"mp3-duration": "^1.1.0",
"util": "^0.12.3",
"video-stitch": "^1.6.0",

0 comments on commit 18b1ee2

Please sign in to comment.