mono/packages/media/ref/yt-dlp/dist/__tests__/mp3.test.js

71 lines
2.9 KiB
JavaScript

import { describe, it, expect, afterAll, beforeAll } from 'vitest';
import { YtDlp } from '../ytdlp.js';
import * as fs from 'node:fs';
import * as path from 'node:path';
describe('MP3 Extraction Tests', () => {
const testOutputDir = path.join(process.cwd(), 'test-downloads/mp3-test');
const ytdlp = new YtDlp({
output: '%(title)s.%(ext)s'
});
// Short YouTube video by YouTube co-founder
const videoUrl = 'https://www.youtube.com/watch?v=jNQXAC9IVRw';
let downloadedFiles = [];
beforeAll(() => {
// Create the output directory if it doesn't exist
if (!fs.existsSync(testOutputDir)) {
fs.mkdirSync(testOutputDir, { recursive: true });
}
});
afterAll(() => {
// Clean up downloaded files after tests
downloadedFiles.forEach(file => {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
console.log(`Cleaned up test file: ${file}`);
}
});
});
it('should download video and extract audio as MP3', async () => {
// Download the video with MP3 extraction
const downloadOptions = {
outputDir: testOutputDir,
audioOnly: true,
audioFormat: 'mp3',
audioQuality: '0', // best quality
verbose: true
};
const filePath = await ytdlp.downloadVideo(videoUrl, downloadOptions);
downloadedFiles.push(filePath);
console.log(`Downloaded MP3 file: ${filePath}`);
// Verify the file exists
expect(fs.existsSync(filePath)).toBe(true);
// Verify it has an MP3 extension
expect(path.extname(filePath)).toBe('.mp3');
// Verify the file has content (not empty)
const stats = fs.statSync(filePath);
expect(stats.size).toBeGreaterThan(0);
console.log(`MP3 file size: ${stats.size} bytes`);
// Log file info for debugging
console.log(`File details:
- Path: ${filePath}
- Size: ${stats.size} bytes
- Created: ${stats.birthtime}
- Modified: ${stats.mtime}
`);
}, 60000); // Increase timeout to 60 seconds for download to complete
it('should have proper MP3 metadata', async () => {
// Get the most recently downloaded file (from previous test)
const mp3File = downloadedFiles[0];
expect(mp3File).toBeDefined();
// Ensure the file still exists
expect(fs.existsSync(mp3File)).toBe(true);
// Check basic file properties to verify it's a valid audio file
const stats = fs.statSync(mp3File);
// MP3 files should have some minimum size (a few KB at least)
expect(stats.size).toBeGreaterThan(10000); // At least 10KB
// We could do more detailed checks with a media info library
// but that would require additional dependencies
console.log(`Verified MP3 file: ${mp3File} (${stats.size} bytes)`);
});
});
//# sourceMappingURL=mp3.test.js.map