Search code examples
google-chromeaudio

Extract Sound from translate.google.com


Bunenas tardes señores!

For many years I extract sound from google.translate.com by Chrome DevTools and the Network tab. When click the sound button in the Network tab mp3 file appears. I just click on it and download for further educational purposes.

Now (literally today) there only XHR type files with countless character sequences inside square brackets as like as strings in JS arrays. And page itself is quite complicated.

How to extract sound in new circumstance?

  • Some how extract from XHR files.
  • May be some way to record in GNU/Linux sound system.
  • Or something else.

Thank you.


Solution

  • php code doing this:

    <?php
    
    $lang = 'en';
    $text = 'hello world';
    
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => 'https://translate.google.com/_/TranslateWebserverUi/data/batchexecute',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => http_build_query([
            'f.req' => json_encode([
                [
                    [
                        'jQ1olc',
                        json_encode([
                            $text,
                            $lang,
                            null,
                            json_encode(null),
                        ]),
                        null,
                        'generic',
                    ]
                ]
            ]),
        ]),
    ]);
    $response = curl_exec($curl);
    curl_close($curl);
    if ($response && preg_match('#//NE[^\\\\]+#', $response, $matches)) {
        file_put_contents('test.mp3', base64_decode($matches[0]));
    }
    else {
        echo "error\n";
    }