video-to-gif
Version:
A Node.js module that searches YouTube videos by song name and converts clips to animated GIFs
66 lines (52 loc) • 1.95 kB
JavaScript
import { Command } from 'commander';
import path from 'path';
import fs from 'fs';
import { searchYouTube } from './lib/youtube-search.js';
import { downloadVideo } from './lib/video-downloader.js';
import { extractAndConvertToGif } from './lib/video-processor.js';
import { createDirectories, cleanupTempFiles, formatDuration } from './lib/utils.js';
export default async function search_vid_gif(song, options) {
try {
// Ensure directories exist
await createDirectories([options.output, options.temp]);
// Search for the song on YouTube
const searchResults = await searchYouTube(song);
if (!searchResults || searchResults.length === 0) {
console.log('❌ No videos found for the given song name');
return null;
}
const video = searchResults[0];
// Download the video
const videoPath = await downloadVideo(video.videoId, options.temp, options.quality);
// Extract clip and convert to GIF
const gifPath = await extractAndConvertToGif(
videoPath,
options.output,
parseInt(options.duration),
song
);
return gifPath;
// Cleanup temporary files
await cleanupTempFiles(options.temp);
} catch (error) {
console.log('❌ Error:', error.message);
// Cleanup on error
try {
await cleanupTempFiles(options.temp);
} catch (cleanupError) {
console.log('⚠️ Failed to cleanup temporary files:', cleanupError.message);
}
return null;
}
};
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.log('❌ Unhandled Rejection at:', promise, 'reason:', reason);
return null;
});
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
console.log('❌ Uncaught Exception:', error);
return null;
});