video-to-gif
Version:
A Node.js module that searches YouTube videos by song name and converts clips to animated GIFs
56 lines (45 loc) • 1.63 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(title, options,url) {
try {
// Ensure directories exist
await createDirectories([options.output, options.temp]);
// Download the video
const videoPath = await downloadVideo(title, options.temp, url);
// Extract clip and convert to GIF
const gifPath = await extractAndConvertToGif(
videoPath,
options.output,
parseInt(options.duration),
title
);
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;
});