Guest User

Untitled

a guest
Apr 20th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os, re, subprocess
  3. import urllib.request
  4.  
  5. # Add valid domains here
  6. VALID_DOMAINS = [
  7.     "files.catbox.moe",
  8. ]
  9.  
  10. # Add valid sound extensions here
  11. VALID_SOUNDEXTS = [
  12.     "mp3",
  13.     "wav",
  14. ]
  15.  
  16. # The mixed file's extension
  17. OUTPUT_EXTENSION = "mp4"
  18.  
  19. # Don't mess with this
  20. VALID_FILENAME = r"(.+)\[sound=((?:https%3A%2F%2F)?(?:" + "|".join(VALID_DOMAINS) + ")%2F(.+\.(?:" + "|".join(VALID_SOUNDEXTS) + ")))\]\.(png|gif|jpeg|webm|mp4|jpg)"
  21.  
  22. def urldecode(text):
  23.     def subfunc(el):
  24.         return chr(int(el[0][1:], 16))
  25.     return re.sub("%[\da-fA-F]{2}", subfunc, text)
  26.  
  27. # Check if we can call a program with os.system()
  28. def program_exists(prog_name):
  29.     if os.name == 'nt': # for Windows
  30.         syscall = f"WHERE {prog_name}"
  31.         ret, err = subprocess.Popen(syscall, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  32.     if os.name == 'posix':  # for Linux
  33.         syscall = f"which {prog_name}"
  34.         ret, err = subprocess.Popen(syscall, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
  35.     if err or not ret:
  36.         return False
  37.     return True
  38.  
  39. def download_soundfile(url, filename):
  40.     headers = {
  41.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5756.197 Safari/537.36',
  42.         'Accept-Encoding': 'deflate',
  43.         'Accept-Language': 'en-US,en;q=0.5',
  44.         'Cache-Control' : 'no-cache',
  45.     }
  46.     req = urllib.request.Request(url, headers=headers)
  47.     data = urllib.request.urlopen(req)
  48.     with open(filename,'wb') as output:
  49.         output.write(data.read())
  50.  
  51. def system_call(cmd):
  52.     if os.name == 'nt': # for Windows
  53.         ret, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  54.     if os.name == 'posix':  # for Linux
  55.         ret, err = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True).communicate()
  56.     return ret, err
  57.  
  58. def get_song_length(fname):
  59.     cmd = f'ffprobe -hide_banner "{fname}"'
  60.     # For some reason ffprobe returns data in err
  61.     ret, err = system_call(cmd)
  62.     res = re.findall("Duration: (\d+:\d{2}:\d{2}(?:\.\d+))", err.decode('utf-8'))
  63.     if not res:
  64.         return None
  65.     tmp = res[0].split(":")
  66.     hours, minutes, seconds = int(tmp[0]), int(tmp[1]), float(tmp[2])
  67.     return (hours * 3600) + (minutes * 60) + seconds
  68.  
  69. # Check if we have ffmpeg
  70. if not program_exists("ffmpeg"):
  71.     print("ffmpeg couldn't be found")
  72.     exit()
  73.  
  74. for file in os.listdir():
  75.     res = re.match(VALID_FILENAME, file)
  76.     if not res:
  77.         continue
  78.  
  79.     imgname, soundurl, soundfname, ext = res[1], urldecode(res[2]), res[3], res[4]
  80.     if not soundurl.startswith("https://"):
  81.         soundurl = "https://" + soundurl
  82.  
  83.     if not os.path.exists(soundfname):
  84.         print(f"File {soundfname} does not exist, downloading")
  85.         download_soundfile(soundurl, soundfname)
  86.  
  87.     song_length = get_song_length(soundfname)
  88.     video_length = 0
  89.     if(ext in ['mp4', 'webm']):
  90.         video_length = get_song_length(file)
  91.  
  92.     ffmpeg_cmd = f'ffmpeg -hide_banner -i "{file}" -i {soundfname} out-{imgname}.{OUTPUT_EXTENSION}'
  93.     os.system(ffmpeg_cmd)
  94.  
Add Comment
Please, Sign In to add comment