Skip to content

Commit

Permalink
fix: convert the videos to h264 codec & add redo-ing option
Browse files Browse the repository at this point in the history
  • Loading branch information
lastarc committed Feb 4, 2024
1 parent 4d3d38e commit 7b51177
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
23 changes: 18 additions & 5 deletions commands/media/tiktok.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { SlashCommandBuilder } = require('discord.js');
const { ListObjectsV2Command, PutObjectCommand } = require('@aws-sdk/client-s3');
const { ListObjectsV2Command, PutObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3');
const { execSync } = require('child_process');
const fs = require('node:fs');
const S3 = require('../../s3');
Expand All @@ -20,8 +20,13 @@ module.exports = {
.setDescription('Displays an embedded TikTok video.')
.addStringOption(option => option.setName('url')
.setDescription('The TikTok video URL.')
.setRequired(true)), async execute(interaction) {
.setRequired(true))
.addBooleanOption(option => option.setName('force')
.setDescription('Re-download?')),
async execute(interaction) {
const videoUrl = interaction.options.getString('url');
const force = interaction.options.getBoolean('force') || false;
console.log({ videoUrl, force });

const replyMsg = await interaction.reply('Processing TikTok link...');

Expand All @@ -41,11 +46,19 @@ module.exports = {

console.log(data);

if (data.KeyCount === 0) {
if (data.KeyCount !== 0 && force) {
console.log('Existing video with `force`, deleting');
await S3.send(new DeleteObjectCommand({
Bucket: S3_BUCKET_NAME,
Key: data.Contents[0].Key,
}));
}

if (data.KeyCount === 0 || force) {
// download the video
execSync('mkdir -p tmp/tiktok');
try {
execSync(`yt-dlp -q -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best -o ./tmp/tiktok/${hash}.mp4 ` + videoUrl, {
execSync(`yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best -S vcodec:h264 -o ./tmp/tiktok/${hash}.mp4 ` + videoUrl, {
stdio: 'inherit',
});
} catch (e) {
Expand All @@ -57,7 +70,7 @@ module.exports = {
// get width and height
let width = 0, height = 0;
try {
const res = '' + execSync(`ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json ./tmp/tiktok/${hash}.mp4`);
const res = execSync(`ffprobe -v error -select_streams v:0 -show_entries stream=width,height,codec_name -of json ./tmp/tiktok/${hash}.mp4`).toString();
console.log(res);
const stream = JSON.parse(res).streams[0];
width = stream.width;
Expand Down
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,24 @@ client.on(Events.InteractionCreate, async interaction => {
});

client.on(Events.MessageCreate, async message => {
const content = message.content;
let msgRef = null;

try {
msgRef = await message.fetchReference();
} catch (e) {
// do nothing
}

let content = message.content;
let force = null;

if (content.toLocaleLowerCase().trim() === 'liben redo' && msgRef) {
content = msgRef.content;
force = true;

message = msgRef;
}

const videoUrlMatch = content.match(/https:\/\/(?:m|www|vm)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|&item_id=)(\d+))|\w+)/gim);

if (!videoUrlMatch) {
Expand All @@ -72,6 +89,7 @@ client.on(Events.MessageCreate, async message => {
client.emit(Events.InteractionCreate, {
isChatInputCommand: () => true, client: client, commandName: 'tiktok', options: {
getString: () => videoUrl,
getBoolean: () => force,
}, reply: message.reply.bind(message),
});
});
Expand Down

0 comments on commit 7b51177

Please sign in to comment.