ShowerLoop-cc/fix-video-types.js

91 lines
2.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* Video MIME Type Fixer
*
* This script updates the HLS video source MIME types in HTML files
* to use the standard video/mp4 fallback type for all browsers.
*/
const fs = require('fs');
const path = require('path');
// Function to find all HTML files in a directory recursively
function findHtmlFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
findHtmlFiles(filePath, fileList);
} else if (path.extname(file).toLowerCase() === '.html') {
fileList.push(filePath);
}
});
return fileList;
}
// Function to update HLS mime types in an HTML file
function updateHlsMimeTypes(filePath) {
try {
let html = fs.readFileSync(filePath, 'utf8');
const originalHtml = html;
// Replace all HLS MIME types with video/mp4 which is more widely supported
html = html.replace(/type=["']application\/x-mpegurl["']/gi, 'type="video/mp4"');
html = html.replace(/type=["']application\/vnd\.apple\.mpegurl["']/gi, 'type="video/mp4"');
// Add the script include for the fixed video initialization
if (!html.includes('video-init-fixed.min.js')) {
html = html.replace('</body>', '<script src="/js/video-init-fixed.min.js"></script></body>');
}
// Only write file if changes were made
if (html !== originalHtml) {
fs.writeFileSync(filePath, html);
console.log(`Updated: ${filePath}`);
return true;
} else {
console.log(`No changes needed: ${filePath}`);
return false;
}
} catch (err) {
console.error(`Error processing file ${filePath}: ${err.message}`);
return false;
}
}
// Main function
function main() {
if (process.argv.length < 3) {
console.error('Error: Please provide a directory path.');
process.exit(1);
}
const directoryPath = process.argv[2];
try {
console.log(`\nScanning directory: ${directoryPath}`);
const htmlFiles = findHtmlFiles(directoryPath);
console.log(`Found ${htmlFiles.length} HTML files.`);
let updatedCount = 0;
htmlFiles.forEach(filePath => {
if (updateHlsMimeTypes(filePath)) {
updatedCount++;
}
});
console.log(`\nUpdated ${updatedCount} of ${htmlFiles.length} HTML files.`);
} catch (err) {
console.error(`Error: ${err.message}`);
process.exit(1);
}
}
main();