89 líneas
3.3 KiB
JavaScript
89 líneas
3.3 KiB
JavaScript
const { IMG2MP3 } = require('../src/index');
|
|
const path = require('path');
|
|
|
|
async function example() {
|
|
const img2mp3 = new IMG2MP3();
|
|
|
|
console.log('🎵 IMG2MP3 Example Usage\n');
|
|
|
|
try {
|
|
// Using the actual files in the examples directory
|
|
const imagePath = path.join(__dirname, 'risitas.jpg');
|
|
const mp3Path = path.join(__dirname, 'chiquito-cobarde.mp3');
|
|
const outputPath = path.join(__dirname, 'risitas_with_chiquito.i2m');
|
|
|
|
console.log('1. Encoding MP3 into image...');
|
|
console.log(` Image: ${path.basename(imagePath)}`);
|
|
console.log(` Audio: ${path.basename(mp3Path)}`);
|
|
|
|
const encodeResult = await img2mp3.encode(imagePath, mp3Path, outputPath);
|
|
console.log('✅ Encoding successful!');
|
|
console.log(` Original image: ${formatBytes(encodeResult.originalImageSize)}`);
|
|
console.log(` MP3 file: ${formatBytes(encodeResult.mp3Size)}`);
|
|
console.log(` Output size: ${formatBytes(encodeResult.outputSize)}`);
|
|
console.log(` Container image: ${encodeResult.imageSize}\n`);
|
|
|
|
console.log('2. Getting file information...');
|
|
const info = await img2mp3.getInfo(outputPath);
|
|
if (info.isValid) {
|
|
console.log('✅ Valid .i2m file detected!');
|
|
console.log(` Embedded audio: ${formatBytes(info.mp3Size)}`);
|
|
console.log(` Original image: ${formatBytes(info.originalImageSize)}`);
|
|
console.log(` Container dimensions: ${info.dimensions}\n`);
|
|
}
|
|
|
|
console.log('3. Playing embedded audio...');
|
|
console.log(' (Press Ctrl+C to stop playback)');
|
|
try {
|
|
await img2mp3.play(outputPath);
|
|
console.log('✅ Playback finished!\n');
|
|
} catch (playError) {
|
|
console.log(`⚠️ Playback failed: ${playError.message}`);
|
|
console.log(' (This is normal if no audio player is installed)\n');
|
|
}
|
|
|
|
console.log('4. Decoding back to separate files...');
|
|
const extractedImagePath = path.join(__dirname, 'extracted_risitas.png');
|
|
const extractedMp3Path = path.join(__dirname, 'extracted_chiquito.mp3');
|
|
|
|
const decodeResult = await img2mp3.decode(
|
|
outputPath,
|
|
extractedImagePath,
|
|
extractedMp3Path
|
|
);
|
|
console.log('✅ Decoding successful!');
|
|
console.log(` Extracted image: ${path.basename(extractedImagePath)} (${formatBytes(decodeResult.extractedImageSize)})`);
|
|
console.log(` Extracted audio: ${path.basename(extractedMp3Path)} (${formatBytes(decodeResult.extractedMp3Size)})`);
|
|
|
|
console.log('\n🎉 Example completed successfully!');
|
|
console.log('\nGenerated files:');
|
|
console.log(`📁 ${path.basename(outputPath)} - The image with embedded audio`);
|
|
console.log(`🖼️ ${path.basename(extractedImagePath)} - Extracted image`);
|
|
console.log(`🎵 ${path.basename(extractedMp3Path)} - Extracted audio`);
|
|
|
|
} catch (error) {
|
|
console.error('❌ Error:', error.message);
|
|
} finally {
|
|
// Clean up any temporary files
|
|
img2mp3.cleanup();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Format bytes to human readable format
|
|
*/
|
|
function formatBytes(bytes, decimals = 2) {
|
|
if (bytes === 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
const dm = decimals < 0 ? 0 : decimals;
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
|
|
}
|
|
|
|
// Run the example
|
|
example().catch(console.error);
|