Skip to content

Commit

Permalink
convewr vdoe added
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostboats committed Jul 22, 2024
1 parent 60d4213 commit 382d3b1
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 0 deletions.
143 changes: 143 additions & 0 deletions commands/convertVideoToGif.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const ffmpeg = require('fluent-ffmpeg');
const ffmpegPath = require('ffmpeg-static');

const { getConfig } = require('../support_files/config');
const { rootModPath } = getConfig();

console.log("FFmpeg Path:", ffmpegPath);
console.log("Type of FFmpeg Path:", typeof ffmpegPath);

if (typeof ffmpegPath === 'string') {
ffmpeg.setFfmpegPath(ffmpegPath);
} else {
console.error("FFmpeg Path is not a string");
}

// Command to open the video conversion webview
const convertVideoToGifCommand = vscode.commands.registerCommand('bg3-mod-helper.convertVideoToGif', async function () {
const panel = vscode.window.createWebviewPanel(
'videoConversion',
'Video Conversion',
vscode.ViewColumn.One,
{ enableScripts: true }
);

const videoFiles = await getAllVideoFiles(rootModPath);

panel.webview.html = getWebviewContent(videoFiles);

// Handle messages from the webview
panel.webview.onDidReceiveMessage(async message => {
switch (message.command) {
case 'convert':
await convertVideoToGifFile(message.videoPath, message.gifPath);
break;
case 'convertAll':
for (const video of videoFiles) {
const gifPath = video.replace(path.extname(video), '.gif');
await convertVideoToGifFile(video, gifPath);
}
break;
case 'selectFile':
const options = {
canSelectMany: false,
openLabel: 'Select a video file',
filters: { 'Video files': ['mp4', 'mkv', 'avi', 'mov'] }
};
const fileUri = await vscode.window.showOpenDialog(options);
if (fileUri && fileUri[0]) {
const videoPath = fileUri[0].fsPath;
const gifPath = videoPath.replace(path.extname(videoPath), '.gif');
await convertVideoToGifFile(videoPath, gifPath);
}
break;
}
});
});

async function getAllVideoFiles(dir) {
let files = [];
const items = await fs.promises.readdir(dir, { withFileTypes: true });
for (const item of items) {
const fullPath = path.join(dir, item.name);
if (item.isDirectory()) {
files = files.concat(await getAllVideoFiles(fullPath));
} else if (/\.(mp4|mkv|avi|mov)$/i.test(item.name)) {
files.push(fullPath);
}
}
return files;
}

function getWebviewContent(videoFiles) {
const videoFileItems = videoFiles.map(file => `
<tr>
<td>${path.basename(file)}</td>
<td>${file}</td>
<td><button onclick="convert('${file.replace(/\\/g, '\\\\')}', '${file.replace(path.extname(file), '.gif').replace(/\\/g, '\\\\')}')">Convert</button></td>
</tr>
`).join('');

return `
<html>
<body>
<h1>Video Conversion</h1>
<table>
<tr>
<th>Name</th>
<th>Path</th>
<th>Action</th>
</tr>
${videoFileItems}
</table>
<button onclick="convertAll()">Convert All</button>
<button onclick="selectFile()">Find Video File on Computer</button>
<script>
const vscode = acquireVsCodeApi();
function convert(videoPath, gifPath) {
vscode.postMessage({ command: 'convert', videoPath: videoPath, gifPath: gifPath });
}
function convertAll() {
vscode.postMessage({ command: 'convertAll' });
}
function selectFile() {
vscode.postMessage({ command: 'selectFile' });
}
</script>
</body>
</html>
`;
}

async function convertVideoToGifFile(inputPath, outputPath) {
return new Promise((resolve, reject) => {
const normalizedInput = path.normalize(inputPath);
const normalizedOutput = path.normalize(outputPath);

console.log("Normalized Input Path:", normalizedInput);
console.log("Normalized Output Path:", normalizedOutput);

ffmpeg(normalizedInput)
.outputOptions([
'-vf', 'fps=24', // Increase fps for smoother animation
'-gifflags', 'transdiff',
'-y', // Overwrite output files without asking
'-q:v', '5' // Set quality level (lower is better, 0 is the best quality)
])
.save(normalizedOutput)
.on('end', () => {
vscode.window.showInformationMessage(`GIF created: ${normalizedOutput}`);
resolve();
})
.on('error', (err) => {
vscode.window.showErrorMessage(`Error: ${err.message}`);
reject(err);
});
});
}


module.exports = { convertVideoToGifCommand };
3 changes: 3 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ let packModImport,
xmlMergerCommand,
symlinkerCommand,
indentXmlFilesCommand,
convertVideoToGifCommand,
debugCommand,
debug2Command,
unpackGameDataCommand,
Expand Down Expand Up @@ -108,6 +109,7 @@ function setCommands() {
organizeDataFilesCommand = require('./commands/organizeDataFiles');
symlinkerCommand = require('./commands/symlinker');
indentXmlFilesCommand = require('./commands/indentXmlFiles');
convertVideoToGifCommand = require('./commands/convertVideoToGif');

// debug commands
debugCommand = require('./commands/debug');
Expand Down Expand Up @@ -226,6 +228,7 @@ function aSimpleDataProvider() {
{ label: 'Generate Folder Structure', command: 'bg3-mod-helper.createModTemplate' },
{ label: 'Atlas Generator (Supply a folder of icons to make an atlas and its corresponding .dds with those icons, as well as its merged.lsx)', command: 'bg3-mod-helper.createAtlas' },
{ label: 'BBCode/Markdown Editor ', command: 'bg3-mod-helper.textEditorTool'},
{ label: 'Convert Video to GIF', command: 'bg3-mod-helper.convertVideoToGif' },
{ label: 'Version Generator', command: 'bg3-mod-helper.versionGenerator' },
{ label: 'Merge Xmls', command: 'bg3-mod-helper.xmlMerger' },
{ label: 'Add/Remove Symlink (in development)', command: 'bg3-mod-helper.symlinker' },
Expand Down
119 changes: 119 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@
"dependencies": "npm install"
},
"dependencies": {
"ffmpeg-static": "^5.2.0",
"fluent-ffmpeg": "^2.1.3",
"jszip": "^3.10.1",
"log4js": "^6.9.1",
"magickwand.js": "^1.1.0",
Expand Down

0 comments on commit 382d3b1

Please sign in to comment.