I'm trying to decode the following string in Python:
61PGlmcmFtZSB0aXRsZT0ib2sgdmlkZW8gcGxheWVyIiBzcmM9Ii8vb2sucnUvdmlkZW9lbWJlZC82MzI5MjYyOTM0NjkwP2F1dG9wbGF5PTEiIGlkPSJwbGFjZWhvbGRlciIgYWxsb3d0cmFuc3BhcmVuY3k9InRydWUiIHNjcm9sbGJhcnM9Im5vIiB0cmFuc3BhcmVudD0ieWVzIiBmcmFtZWJvcmRlcj0iMCIgYWxsb3c9ImVuY3J5cHRlZC1tZWRpYSAqOyBhdXRvcGxheTsgZnVsbHNjcmVlbiIgd2lkdGg9IjEwMCUiIGhlaWdodD0iNDgwIiBzY3JvbGxpbmc9Ik5vIiAgYWxsb3dmdWxsc2NyZWVuPjwvaWZyYW1lPg==
However, I'm getting this error with "utf-8" (other encoding options give invalid output):
Text\decode test.py", line 7, in <module>
decoded_string = decoded_bytes.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 0: invalid continuation byte
The correct decoded string should look like:
<a iframe title="ok video player" src="//ok.ru/videoembed/6329262934690?autoplay=1" id="player" allowfullscreen="true" fullscreen="" width="100%" height="480" scrolling="no" allow="encrypted-media" frameborder="0" allowfullscreen></iframe>
I have tried multiple online decoding tools as well but I didn't get any correct answer. Only ChatGPT was able to give me the correct decoded string; However, when I ask for the procedure, I keep on getting the same python code I'm using as well:
import base64
encoded_string = "61PGlmcmFtZSB0aXRsZT0ib2sgdmlkZW8gcGxheWVyIiBzcmM9Ii8vb2sucnUvdmlkZW9lbWJlZC82MzI5MjYyOTM0NjkwP2F1dG9wbGF5PTEiIGlkPSJwbGFjZWhvbGRlciIgYWxsb3d0cmFuc3BhcmVuY3k9InRydWUiIHNjcm9sbGJhcnM9Im5vIiB0cmFuc3BhcmVudD0ieWVzIiBmcmFtZWJvcmRlcj0iMCIgYWxsb3c9ImVuY3J5cHRlZC1tZWRpYSAqOyBhdXRvcGxheTsgZnVsbHNjcmVlbiIgd2lkdGg9IjEwMCUiIGhlaWdodD0iNDgwIiBzY3JvbGxpbmc9Ik5vIiAgYWxsb3dmdWxsc2NyZWVuPjwvaWZyYW1lPg=="
decoded_bytes = base64.b64decode(encoded_string)
decoded_string = decoded_bytes.decode("utf-8")
print(decoded_string)
What is wrong here?
From the website you share, there is a method called setVideo
.
To decode the base64, it does this operation:
window.atob(track.toString().substring(2));
In python, here is the equivalent
import base64
encoded_string = "61PGlmcmFtZSB0aXRsZT0ib2sgdmlkZW8gcGxheWVyIiBzcmM9Ii8vb2sucnUvdmlkZW9lbWJlZC82MzI5MjYyOTM0NjkwP2F1dG9wbGF5PTEiIGlkPSJwbGFjZWhvbGRlciIgYWxsb3d0cmFuc3BhcmVuY3k9InRydWUiIHNjcm9sbGJhcnM9Im5vIiB0cmFuc3BhcmVudD0ieWVzIiBmcmFtZWJvcmRlcj0iMCIgYWxsb3c9ImVuY3J5cHRlZC1tZWRpYSAqOyBhdXRvcGxheTsgZnVsbHNjcmVlbiIgd2lkdGg9IjEwMCUiIGhlaWdodD0iNDgwIiBzY3JvbGxpbmc9Ik5vIiAgYWxsb3dmdWxsc2NyZWVuPjwvaWZyYW1lPg=="
decoded_track = base64.b64decode(encoded_string[2:]).decode('utf-8')
print(decoded_track)