Please bookmark this page to avoid losing your image tool!

YouTube Video To Image Extractor Tool

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
async function processImage(originalImg, youtubeVideoUrl = "") {
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // Helper: Extracts a YouTube Video ID from standard YouTube URL formats
    function getYoutubeId(url) {
        if (!url || typeof url !== 'string') return null;
        const match = url.match(/(?:youtu\.be\/|youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=))([^"&?\/\s]{11})/i);
        return match ? match[1] : null;
    }
    
    // Helper: Crops horizontal/vertical black bars (commonly found in YouTube thumbnails)
    function autoCropBlackBars(cvs) {
        const context = cvs.getContext('2d');
        const width = cvs.width;
        const height = cvs.height;
        let imgData;

        try {
            // Might throw an error if the originalImg lacks cross-origin permissions
            imgData = context.getImageData(0, 0, width, height);
        } catch (e) {
            console.warn("CORS restriction on image data. Returning uncropped image.");
            return cvs; 
        }

        const data = imgData.data;
        const threshold = 15; // Handle minor compression noise (0-255)

        // Find Top
        let top = 0;
        topLoop: for (let y = 0; y < height / 2; y++) {
            for (let x = 0; x < width; x++) {
                const i = (y * width + x) * 4;
                if (data[i] > threshold || data[i + 1] > threshold || data[i + 2] > threshold) break topLoop;
            }
            top++;
        }

        // Find Bottom
        let bottom = height - 1;
        bottomLoop: for (let y = height - 1; y > height / 2; y--) {
            for (let x = 0; x < width; x++) {
                const i = (y * width + x) * 4;
                if (data[i] > threshold || data[i + 1] > threshold || data[i + 2] > threshold) break bottomLoop;
            }
            bottom--;
        }

        // Find Left
        let left = 0;
        leftLoop: for (let x = 0; x < width / 2; x++) {
            for (let y = 0; y < height; y++) {
                const i = (y * width + x) * 4;
                if (data[i] > threshold || data[i + 1] > threshold || data[i + 2] > threshold) break leftLoop;
            }
            left++;
        }

        // Find Right
        let right = width - 1;
        rightLoop: for (let x = width - 1; x > width / 2; x--) {
            for (let y = 0; y < height; y++) {
                const i = (y * width + x) * 4;
                if (data[i] > threshold || data[i + 1] > threshold || data[i + 2] > threshold) break rightLoop;
            }
            right--;
        }

        const newWidth = right - left + 1;
        const newHeight = bottom - top + 1;

        if (newWidth > 0 && newHeight > 0 && (newWidth < width || newHeight < height)) {
            const croppedCanvas = document.createElement('canvas');
            croppedCanvas.width = newWidth;
            croppedCanvas.height = newHeight;
            croppedCanvas.getContext('2d').drawImage(cvs, left, top, newWidth, newHeight, 0, 0, newWidth, newHeight);
            return croppedCanvas;
        }

        return cvs;
    }

    const ytId = getYoutubeId(youtubeVideoUrl);

    // Attempt to download the YouTube thumbnail if a valid URL is present
    if (ytId) {
        const loadThumbnail = (url) => new Promise((resolve) => {
            const img = new Image();
            img.crossOrigin = 'anonymous';
            img.onload = () => resolve(img);
            img.onerror = () => resolve(null);
            img.src = url;
        });

        // Try downloading down the resolution scale sizes (maxresdefault often missing)
        const resolutions = ['maxresdefault.jpg', 'hqdefault.jpg', 'mqdefault.jpg', '0.jpg'];
        for (let res of resolutions) {
            const img = await loadThumbnail(`https://img.youtube.com/vi/${ytId}/${res}`);

            // YouTube sometimes returns a 120x90 blank/error image for missing HD thumbnails
            if (img && img.width > 120) {
                canvas.width = img.width;
                canvas.height = img.height;
                ctx.drawImage(img, 0, 0);
                return autoCropBlackBars(canvas);
            }
        }
    }

    // Fallback: If no YouTube URL string provided but a dropped Image / Video frame was passed
    if (originalImg && originalImg.width > 0 && originalImg.height > 0) {
        canvas.width = originalImg.width;
        canvas.height = originalImg.height;
        ctx.drawImage(originalImg, 0, 0);
        return autoCropBlackBars(canvas);
    }

    // Final Fallback: Canvas placeholder Error State
    canvas.width = 640;
    canvas.height = 360;
    ctx.fillStyle = '#1e1e1e';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    
    ctx.fillStyle = '#ffffff';
    ctx.font = '20px Arial';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText('Please drag & drop a valid YouTube URL or Image', canvas.width / 2, canvas.height / 2);
    
    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

This tool allows users to extract high-quality images from YouTube videos by providing a video URL. It automatically identifies the video ID, retrieves the best available thumbnail resolution, and includes an intelligent auto-crop feature that removes black bars often found in video thumbnails. Additionally, the tool can process uploaded images directly. It is useful for content creators, designers, or researchers who need to quickly capture and clean up visual assets from YouTube content for use in presentations, social media, or other creative projects.

Leave a Reply

Your email address will not be published. Required fields are marked *