organise-videos.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. import shutil
  3. # Find the files in the current directory.
  4. current_dir_files = os.listdir()
  5. # Find the path of the current directory.
  6. current_dir_path = os.getcwd()
  7. # The Instagram videos are in MP4 format.
  8. video_format = ['mp4']
  9. # Find the MP4 files and save them in a variable called 'filenames'.
  10. filenames = [filename for filename in current_dir_files if filename.split('.')[-1] in video_format]
  11. # We want a dictionary where the filenames are the keys and the usernames are the values.
  12. filenames_to_usernames = {}
  13. # Populate the dictionary by going through each filename and removing the undesired characters, leaving just the usernames.
  14. for filename in filenames:
  15. filename_parts = filename.split('_')[1:-3]
  16. usernames = '_'.join(filename_parts)
  17. filenames_to_usernames[filename] = usernames
  18. # The usernames are the values of the filenames_to_usernames dictionary.
  19. usernames = filenames_to_usernames.values()
  20. # Make a folder for each username.
  21. for username in usernames:
  22. if not os.path.isdir(username):
  23. os.mkdir(username)
  24. # We want a dictionary where the usernames are the keys and the associated folder paths are the keys.
  25. username_to_folder = {}
  26. # Populate the dictionary.
  27. for username in usernames:
  28. folder_paths = current_dir_path + "\\" + username
  29. username_to_folder[username] = folder_paths
  30. # Move the videos into the folders
  31. for filename, username in filenames_to_usernames.items():
  32. shutil.move(filename, username)
  33. num_videos_moved = len(filenames_to_usernames.keys())
  34. print("{} videos moved successfully.".format(num_videos_moved))