Skip to content

Instantly share code, notes, and snippets.

@serrasqueiro
Last active March 17, 2025 21:47
Show Gist options
  • Save serrasqueiro/f54f224114200f9964483981b6397000 to your computer and use it in GitHub Desktop.
Save serrasqueiro/f54f224114200f9964483981b6397000 to your computer and use it in GitHub Desktop.
dump Youtube video ID - sample!
""" This is simply checking the consistency of the methods.
"""
from fromdecimalfix import to_youtubeid
from youtubeid import youtube_id_to_integer
NAMES = {
1: {
"watch": ("3pYsfaQeMRs", "Justin Bieber - Somebody (Live from Paris)"),
"tags": [
"#LiveFromParis",
"#JustinBieber",
],
},
2: {
"watch": ("sBjNRLiLO5Y", "Justin Bieber - Live from Paris (Livestream)"),
},
}
def main():
""" List all 'watch' at names. """
names = NAMES
alist = [names[key]["watch"][0] for key in sorted(names)]
assert checker(alist), "ok!"
def checker(ids):
""" Sample checker """
for youtube_id in ids:
a_num = youtube_id_to_integer(youtube_id)
watch = to_youtubeid(a_num)
print("Checking:", youtube_id + ", as:", a_num, "; is:", watch)
assert watch == youtube_id, youtube_id
return True
if __name__ == "__main__":
main()
""" Base64URL (from decimal to YouTube video IDs)
see also: youtubeid.py
"""
import base64
def main():
""" Sample for Justin Bieber at the rooftop in Paris, video """
youtube_id = "3pYsfaQeMRs"
sample = 64156224164250305644
calc = to_youtubeid(sample)
print(
f"The integer value of the YouTube ID '{youtube_id}' is: {sample}",
"\n" + f"https://www.youtube.com/watch?v={youtube_id}",
end="\n\n",
)
print(
youtube_id, "\n" + calc,
)
assert calc == youtube_id, calc
def to_youtubeid(a_num:int) -> str:
""" Returns the Base64URL equivalent to the integer 'a_num'. """
_, string = decimal_to_base64url(a_num)
return string
def decimal_to_base64url(a_num):
""" convert Convert the decimal number to string. """
a_max = 64 ** 11
assert a_num < a_max, f"Too big: {a_num}"
byte_array = a_num.to_bytes((a_max.bit_length() + 7)// 8, byteorder='big')
# Encode the bytes to Base64URL
string = base64.urlsafe_b64encode(byte_array).decode("ascii")
# Strip any padding ("=") as it's not required for Base64URL
astr = string.rstrip('=')
return astr, string
if __name__ == "__main__":
main()
""" Base64URL (from decimal to YouTube video IDs) - fixed version
see also: youtubeid.py
"""
URL_BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
URL_Y_SAMPLE = 64156224164250305644
def main():
""" Sample for Justin Bieber at the rooftop in Paris, video """
youtube_id = "3pYsfaQeMRs"
sample = URL_Y_SAMPLE
calc = to_youtubeid(sample)
print(
f"The integer value of the YouTube ID '{youtube_id}' is: {sample}",
"\n" + f"https://www.youtube.com/watch?v={youtube_id}",
end=calc + "\n",
)
print(youtube_id + "\n" + calc)
assert calc == youtube_id, calc
def to_youtubeid(a_num:int) -> str:
""" Returns the Base64URL equivalent to the integer 'a_num'. """
return decimal_to_base64url(a_num)
def decimal_to_base64url(a_num, alphabet=""):
""" Correct conversion of the decimal number to string. """
alphabet = alphabet if alphabet else URL_BASE64_ALPHABET
# Convert decimal to Base64URL manually
if a_num == 0:
return alphabet[0]
result = []
base = len(alphabet)
assert base == 64, base
while a_num > 0:
remainder = a_num % base
result.append(alphabet[remainder])
a_num //= base
# Reverse to correct order and return
return ''.join(reversed(result))
if __name__ == "__main__":
main()
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
""" nibbler.py (c)2025 Henrique Moreira
"""
import fromdecimalfix
from fromdecimalfix import URL_Y_SAMPLE
def main():
""" Simple example. """
decimal_value = 64 ** 11
decimal_value -= 1
sampler((decimal_value, URL_Y_SAMPLE, 1))
other = 2 ** (18 * 4)
assert other > decimal_value, "18 hex nibbles are enough."
def sampler(nums):
""" Shows the 'nums' samples. """
print("It takes 18 nibbles to represent 64^11", end="\n\n")
for aval in nums:
youtube_id = fromdecimalfix.to_youtubeid(aval)
print(
eighteen_addr(aval),
f"{youtube_id:.<11}",
f"is: {aval:018x} ({aval}d)",
)
def eighteen_addr(decimal_value):
""" Convert to hexadecimal 18-notation. """
s_hex = f"{decimal_value:018x}"
#hex_value = hex(decimal_value)[2:] # Remove '0x' prefix
# Format as IPv6like-18notation:
like = ':'.join(s_hex[i:i+6] for i in range(0, len(s_hex), 6))
return like
if __name__ == "__main__":
main()
# https://www.youtube.com/watch?v=p_4M4TKTydM
p_4M4TKTydM
# https://www.youtube.com/watch?v=3pYsfaQeMRs&list=PLinEjEea9QPfm0rjlxAKuy-cqN0QlYsI2&index=2
3pYsfaQeMRs,list=PLinEjEea9QPfm0rjlxAKuy-cqN0QlYsI2,index=2
# https://www.youtube.com/watch?v=sBjNRLiLO5Y&list=PLinEjEea9QPfm0rjlxAKuy-cqN0QlYsI2&index=6
sBjNRLiLO5Y,list=PLinEjEea9QPfm0rjlxAKuy-cqN0QlYsI2,index=6
@ Justin Bieber - Live from Paris (Livestream)
""" Base64URL (or YouTube video IDs)
Uppercase letters: A..Z (26 characters)
Lowercase letters: a..z (26 characters)
Numbers: 0..9 (10 characters)
Special characters: Hyphen (-) and underscore (_)
This gives a total of 64 possible characters,
making it efficient for generating unique video IDs that are case-sensitive.
"""
SAMPLE_INT = 64156224164250305644
def main():
""" Sample for Justin Bieber at the rooftop in Paris, video """
youtube_id = "3pYsfaQeMRs"
integer_value = youtube_id_to_integer(youtube_id)
newline = "\n\n"
# https://www.youtube.com/watch?v=3pYsfaQeMRs
print(
f"The integer value of the YouTube ID '{youtube_id}' is: {integer_value}"
f"{newline}https://www.youtube.com/watch?v={youtube_id}",
)
assert integer_value == SAMPLE_INT, youtube_id
### Sample converter
def youtube_id_to_integer(youtube_id):
""" Returns an integer from Base64URL encoding alphabet """
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
base = len(alphabet)
# Convert the YouTube ID to an integer
result = 0
for char in youtube_id:
result = result * base + alphabet.index(char)
return result
if __name__ == "__main__":
main()
@ljcool2006
Copy link

Maybe you should add a decryptor when the DES or 3DES (not sure which) key used for encrypting video IDs from 2005 to 2016 eventually gets found. I have some information about the key if it's of any use. I should note that the YouTube video ID with the decrypted integer of 578721382704613384 (basically 08 08 08 08 08 08 08 08 in hex form) is GU5U2spHI_4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment