JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

codeberg.org/vile | JustPaste.app
6 days ago3 views
👨‍💻Programming

codeberg.org/vile

<?php
// resolve.php - High-Reliability Audio Stream Resolver

header('Content-Type: application/json');

$artist = $_GET['artist'] ?? '';
$title = $_GET['title'] ?? '';

if (empty($artist) && empty($title)) {
    echo json_encode(['error' => 'Missing artist or title parameters']);
    exit;
}

// Clean and format query string
$cleanArtist = html_entity_decode($artist, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$cleanTitle = html_entity_decode($title, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$query = urlencode(trim("{$cleanArtist} {$cleanTitle} audio"));

// Functional Invidious Instance Pool
$invidiousInstances = [
    "https://inv.tux.pizza",
    "https://invidious.nerdvpn.de",
    "https://yewtu.be",
    "https://invidious.drgns.space"
];

function httpGet(string $url): ?string {
    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
        CURLOPT_SSL_VERIFYPEER => false
    ]);
    $res = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return ($code === 200 && $res) ? $res : null;
}

// Helper to extract audio stream URL from video details object
function extractAudioUrl(array $videoDetails): ?string {
    // Check primary audioStreams array
    if (!empty($videoDetails['audioStreams'])) {
        usort($videoDetails['audioStreams'], function($a, $b) {
            return ($a['bitrate'] ?? 0) <=> ($b['bitrate'] ?? 0);
        });
        return $videoDetails['audioStreams'][0]['url'] ?? null;
    }

    // Fallback: Check adaptiveFormats array for audio/ MIME types
    if (!empty($videoDetails['adaptiveFormats'])) {
        $audioFormats = array_filter($videoDetails['adaptiveFormats'], function($format) {
            return isset($format['type']) && strpos($format['type'], 'audio/') === 0;
        });

        if (!empty($audioFormats)) {
            usort($audioFormats, function($a, $b) {
                return ($a['bitrate'] ?? 0) <=> ($b['bitrate'] ?? 0);
            });
            $first = reset($audioFormats);
            return $first['url'] ?? null;
        }
    }

    return null;
}

// Strategy 1: Invidious Instances
foreach ($invidiousInstances as $instance) {
    $searchUrl = "{$instance}/api/v1/search?q={$query}&type=video";
    $searchRes = httpGet($searchUrl);
    
    if (!$searchRes) continue;
    
    $searchResults = json_decode($searchRes, true);
    if (empty($searchResults) || !isset($searchResults[0]['videoId'])) continue;

    $videoId = $searchResults[0]['videoId'];
    $videoUrl = "{$instance}/api/v1/videos/{$videoId}";
    $videoRes = httpGet($videoUrl);

    if (!$videoRes) continue;

    $videoDetails = json_decode($videoRes, true);
    $streamUrl = extractAudioUrl($videoDetails);

    if ($streamUrl) {
        echo json_encode([
            'video_id' => $videoId,
            'stream_url' => $streamUrl,
            'engine' => 'invidious',
            'instance' => $instance
        ]);
        exit;
    }
}

// Strategy 2: Piped API Mirrors Fallback
$pipedInstances = [
    "https://api.piped.yt",
    "https://pipedapi.mha.fi",
    "https://pipedapi.tokhmi.xyz"
];

foreach ($pipedInstances as $piped) {
    $searchUrl = "{$piped}/search?q={$query}&filter=music_songs";
    $searchRes = httpGet($searchUrl);
    if (!$searchRes) continue;

    $searchResults = json_decode($searchRes, true);
    $items = $searchResults['items'] ?? [];
    if (empty($items)) continue;

    $urlPath = $items[0]['url'] ?? '';
    preg_match('/v=([a-zA-Z0-9_-]+)/', $urlPath, $vMatch);
    $videoId = $vMatch[1] ?? null;

    if (!$videoId) continue;

    $streamUrl = "{$piped}/streams/{$videoId}";
    $streamRes = httpGet($streamUrl);
    if (!$streamRes) continue;

    $streamDetails = json_decode($streamRes, true);
    $audioStreams = $streamDetails['audioStreams'] ?? [];

    if (!empty($audioStreams)) {
        echo json_encode([
            'video_id' => $videoId,
            'stream_url' => $audioStreams[0]['url'],
            'engine' => 'piped',
            'instance' => $piped
        ]);
        exit;
    }
}

echo json_encode(['error' => 'No matching stream found across all backend mirrors']);
← Back to timeline