reddit-memes-to-vid.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. #
  3. # python reddit-memes-to-vid.py <SUBREDDIT>
  4. import requests
  5. import os
  6. import sys
  7. try:
  8. subreddit=sys.argv[1]
  9. except:
  10. print("Please give an argument for a subreddit")
  11. exit()
  12. audio = "C:\\SGZ_Pro\\Hobbys\\Media\\music\\lukrembo - butter (royalty free vlog music) [Ua7Qfc1xu90].mp3"
  13. headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121201 icecat/17.0.1'}
  14. data = requests.get(f"https://www.reddit.com/r/{subreddit}.json?limit=400",headers=headers).json()
  15. if "message" and "error" in data:
  16. print("Had trouble finding this subreddit. Try another one")
  17. quit()
  18. name = 0
  19. format = ".png"
  20. for image in data["data"]["children"]:
  21. url = image["data"]["url"]
  22. if url.startswith("https://i.redd.it/"):
  23. if image["data"]["thumbnail"] == "nsfw":
  24. pass
  25. elif url.endswith(format):
  26. name=name+1
  27. print(name)
  28. with open(str(name)+format,"wb") as f:
  29. f.write(requests.get(image["data"]["url"]).content)
  30. elif url.endswith(".jpg") or url.endswith(".jpeg"):
  31. name=name+1
  32. extension = "."+url.split("/")[3].split(".")[1]
  33. print(name)
  34. with open(str(name)+extension,"wb") as f:
  35. f.write(requests.get(image["data"]["url"]).content)
  36. os.system(f"ffmpeg -y -i {name}{extension} -preset ultrafast {name}{format}")
  37. os.remove(f"{name}{extension}")
  38. os.system(f'ffmpeg -y -framerate 1 -i %d{format} -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:-1:-1:color=black" -acodec copy input.mp4')
  39. os.system(f'ffmpeg -y -i input.mp4 -i "{audio}" -filter_complex \" [1:0] apad \" -shortest output.mp4')
  40. for file in os.listdir():
  41. if file.endswith(".png"):
  42. os.remove(file)
  43. elif file.endswith(".jpg"):
  44. os.remove(file)
  45. elif file.endswith(".jpeg"):
  46. os.remove(file)
  47. os.remove("input.mp4")