zackmawaldi / YouTube-shorts-generator

Automatically download Reddit videos, edit them, and upload them to YouTube.
MIT License
323 stars 62 forks source link

Logging into Reddit... Log in success! Retrieving post info... An error occurred in main() on attempt 10: 'NoneType' object has no attribute 'name' #21

Open axen1 opened 1 year ago

axen1 commented 1 year ago

Logging into Reddit... Log in success! Retrieving post info... An error occurred in main() on attempt 10: 'NoneType' object has no attribute 'name'

zackmawaldi commented 1 year ago

Hey! I did end up rewriting a good bit go the code recently. Can you re-run it with new updated code? If so, please copy-paste entire terminal output.

axen1 commented 1 year ago

Hey Zack the problem persists. Also, this error pops up a lot. [WinError 32] This process cannot access the file because it is being used by another process: 'temp_clips\main_clip.mp4'

zackmawaldi commented 1 year ago

Can you copy paste the entire terminal when the error pops up? I'm hoping there's more information that just this line. My guess it's a windows specific problem, since I tested the code only with Mac and linux machines. @axen1

axen1 commented 1 year ago

Logging into Reddit... Log in success! Retrieving post info... Scraping reddit success! Returning list of info back to main. Video chosen: url https://v.redd.it/dkx9xjvz9iva1
title Plz come in my shop author MelancholyS4D Attempting to download reddit video...

Connecting... Scraping... Downloading and Re-encoding... Video:

[██████████████████████████████████████████████████] [1.91/1.91 MB] Audio: [██████████████████████████████████████████████████] [0.14/0.14 MB] Done Download success! Clip is very close to theoretical aspect ratio! Exact: 0.5638888888888889 Theoretical: 0.5625 An error occurred in main() on attempt 1: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' -> 'temp_clips\output.mp4' An error occurred in main() on attempt 2: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 3: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 4: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 5: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 6: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 7: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 8: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 9: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' An error occurred in main() on attempt 10: [WinError 32] Dosya başka bir işlem tarafından kullanıldığından bu işlem dosyaya erişemiyor: 'temp_clips\main_clip.mp4' Attempted to upload 10 times with no luck... ending script.

zackmawaldi commented 1 year ago

Ok I may have found a solution. It seems like automatic closing might be inconsistent on windows. Try adding these lines, and test the code again. I don't have a windows machine so I can't test it atm.

In render.py, change the render function to this code:

def render(directory, clip_name, output_name, resolution):
    """
    Resizes, centers, and renders a video clip with optional edge blur if needed.

    Args:
        directory (str): Directory path of the clip.
        clip_name (str): Name of the clip file.
        output_name (str): Desired name of the output file.
        resolution (tuple): Tuple of the desired resolution of the video.

    Returns:
        int: 0 for successful rendering and 1 if the video fits the desired format already.
    """
    clip_dir = os.path.join(directory, clip_name)
    main_clip = VideoFileClip(clip_dir)

    exact_ratio = main_clip.size[0] / main_clip.size[1]
    theoretical_ratio = resolution[0] / resolution[1]

    if theoretical_ratio * 0.95 < exact_ratio < theoretical_ratio * 1.05:
        print(f"Clip is very close to theoretical aspect ratio!\n"
              f"Exact: {exact_ratio}\n"
              f"Theoretical: {theoretical_ratio}")

        ##########################################
        main_clip.close() # close file to avoid Windows bugs
        ##########################################

        os.rename(os.path.join(directory, "main_clip.mp4"), os.path.join(directory, "output.mp4"))
        return 1

    # Make background a blurred and darkened version of clip
    bg = VideoFileClip(clip_dir).resize(resolution)
    bg = bg.fx(vfx.colorx, 0.1)
    if config.video['blur']:
        bg = bg.fl_image(blur)

    main_clip = main_clip.resize(width=resolution[0])
    main_clip = main_clip.set_start(0)

    video = CompositeVideoClip([bg, main_clip.set_position("center", "center")])
    video.write_videofile(os.path.join(directory, output_name), audio_codec='aac')

    ##########################################
    main_clip.close() # close file to avoid Windows bugs
    ##########################################

    return 0

Basically just added the line main_clip.close() before renaming the file and end of function. Not closing seems to freak windows permissions for some reason. Could be wrong, though, so let me know if it works so I can added to the code!

axen1 commented 1 year ago

I will try and let you know.