Files
img2mp3/examples/basic_usage.js
2025-09-06 19:11:40 +02:00

63 líneas
2.4 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 {
// Note: You'll need to provide actual image and MP3 files
const imagePath = 'sample_image.jpg'; // Replace with actual image
const mp3Path = 'sample_audio.mp3'; // Replace with actual MP3
const outputPath = 'example_output.i2m';
console.log('1. Encoding MP3 into image...');
// Uncomment when you have actual files:
// const encodeResult = await img2mp3.encode(imagePath, mp3Path, outputPath);
// console.log('✅ Encoding successful!');
// console.log(` Output size: ${encodeResult.outputSize} bytes`);
// console.log(` Container image: ${encodeResult.imageSize}\n`);
console.log('2. Getting file information...');
// Uncomment when you have an actual .i2m file:
// const info = await img2mp3.getInfo(outputPath);
// if (info.isValid) {
// console.log('✅ Valid .i2m file detected!');
// console.log(` Embedded audio: ${info.mp3Size} bytes`);
// console.log(` Original image: ${info.originalImageSize} bytes\n`);
// }
console.log('3. Playing embedded audio...');
// Uncomment when you have an actual .i2m file:
// await img2mp3.play(outputPath);
// console.log('✅ Playback finished!\n');
console.log('4. Decoding back to separate files...');
// Uncomment when you have an actual .i2m file:
// const decodeResult = await img2mp3.decode(
// outputPath,
// 'extracted_image.png',
// 'extracted_audio.mp3'
// );
// console.log('✅ Decoding successful!');
// console.log(` Extracted image: ${decodeResult.extractedImageSize} bytes`);
// console.log(` Extracted audio: ${decodeResult.extractedMp3Size} bytes`);
console.log('\n🎉 Example completed!');
console.log('\nTo run this example with real files:');
console.log('1. Place an image file named "sample_image.jpg" in this directory');
console.log('2. Place an MP3 file named "sample_audio.mp3" in this directory');
console.log('3. Uncomment the code above and run again');
} catch (error) {
console.error('❌ Error:', error.message);
} finally {
// Clean up any temporary files
img2mp3.cleanup();
}
}
// Run the example
example().catch(console.error);