Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Optimize the code and make it shorter by keeping exactly the same functionality. There…

  1. User

    Optimize the code and make it shorter by keeping exactly the same functionality. There is the code :import os import shutil import moviepy.editor as mp import openai import re from openai_helper import openai, generate_description import os from moviepy.editor import * from moviepy.video import fx from moviepy.video.fx import all as fx from PIL import ImageOps #from create_text_clip_overlay import create_text_clip from create_image_with_text import create_image_with_text from TESTINGvideo_creation import create_final_clip

    Set the size of the final video

    size = (1080, 1920)

    Time to delay top attributes

    delayins=5 fps_size=30

    Set the directory where all the files are stored

    dir_path = "C:/Python/VideoCreationTiktok/Source/" dir_path_output = "C:/Python/VideoCreationTiktok/Output/" #File to edit

    TODO: EDIT this

    #main_video_path = os.path.join(dir_path, "main_video1.mp4")

    set the folder path for folder management

    folder_path = "E:/SMART AI BOTS/Selected"

    loop through all video files in the folder

    for filename in os.listdir(folder_path): if filename.endswith(".mp4") or filename.endswith(".avi"): # extract the name, channel, description, and hashtags from the filename name = filename.rsplit(".", 1)[0] channel, desc_hashtags = name.split("-", 1)

        desc_end_index = desc_hashtags.find("#")
        if desc_end_index != -1:
            desc = desc_hashtags[:desc_end_index].strip()
            hashtags = desc_hashtags[desc_end_index:].strip()
        else:
            desc = desc_hashtags.strip()
            hashtags = ""
    
        # print the variables for debugging purposes
        print(f"name: {name}")
        #print(f"channel: {channel}")
        #print(f"desc: {desc}")
        #print(f"hashtags: {hashtags}")
    
        # generate a description using ChatGPT
        prompt = f"Write a short, attention-grabbing, clickbaity description for a video as a statement. Use maximum 8 words and provide only one final result, all capital letters. This is the title: '{desc} {hashtags}'"
        description = generate_description(prompt)
    
        print(f"prompt: {prompt}")
        print(f"description: {description}")
    
        # clean up the description for use as a file name
        clean_description = re.sub(r'[^\w\s#-&:]','',f"{description} {hashtags}")
        print(f"description2: {clean_description}")
        clean_description = re.sub(r"\s{2,}", " ", clean_description.strip())
        clean_description = re.sub(r'#+', '#', clean_description.strip())
        clean_description = re.sub(r':', '-', clean_description.strip())
        trimed_file_name = f"{clean_description}.mp4"
    
        
        print(f"description3 trimmed: {trimed_file_name}")
    
        #TODO:REMOVE
        # trim the video to one second
        video_path = os.path.join(folder_path, filename)
    
        # Get the length of the main video
        main_video_path=f"{folder_path}/{filename}" #path = folder path we sceraping + file name found
        main_video = VideoFileClip(main_video_path)
        main_video_length = main_video.duration
    
        # ALL EDITING GOES HERE
    
        text_center= description
        final_clip=create_final_clip(text_center, main_video_path)
    
        
        #Save final video to final folder
        final_path = os.path.join(folder_path, "Final", trimed_file_name)
    
        final_clip=final_clip.subclip(0,3)
        final_clip.write_videofile(final_path,fps=fps_size)
        final_clip.close()
        main_video.close()
        
        # move the original video to the Used folder
        original_path = os.path.join(folder_path, filename)
        used_path = os.path.join(folder_path, "Used", filename)
        shutil.move(original_path, used_path)
    
  2. Assistant
    import os
    import shutil
    import re
    import moviepy.editor as mp
    from openai_helper import openai, generate_description
    from create_image_with_text import create_image_with_text
    from TESTINGvideo_creation import create_final_clip
    
    size = (1080, 1920)
    delayins = 5
    fps_size = 30
    
    dir_path = "C:/Python/VideoCreationTiktok/Source/"
    dir_path_output = "C:/Python/VideoCreationTiktok/Output/"
    
    folder_path = "E:/SMART AI BOTS/Selected"
    
    for filename in os.listdir(folder_path):
        if filename.endswith((".mp4", ".avi")):
            name = filename.rsplit(".", 1)[0]
            channel, desc_hashtags = name.split("-", 1)
    
            desc_end_index = desc_hashtags.find("#")
            if desc_end_index != -1:
                desc = desc_hashtags[:desc_end_index].strip()
                hashtags = desc_hashtags[desc_end_index:].strip()
            else:
                desc = desc_hashtags.strip()
                hashtags = ""
    
            prompt = f"Write a short, attention-grabbing, clickbaity description for a video as a statement. Use maximum 8 words and provide only one final result, all capital letters. This is the title: '{desc} {hashtags}'"
            description = generate_description(prompt)
    
            clean_description = re.sub(r'[^\w\s#-&:]','', f"{description} {hashtags}")
            clean_description = re.sub(r"\s{2,}", " ", clean_description.strip())
            clean_description = re.sub(r'#+', '#', clean_description.strip())
            clean_description = re.sub(r':', '-', clean_description.strip())
            trimed_file_name = f"{clean_description}.mp4"
    
            video_path = os.path.join(folder_path, filename)
            main_video_path = f"{folder_path}/{filename}"
            main_video = mp.VideoFileClip(main_video_path)
            main_video_length = main_video.duration
    
            text_center = description
            final_clip = create_final_clip(text_center, main_video_path)
    
            final_path = os.path.join(folder_path, "Final", trimed_file_name)
            final_clip = final_clip.subclip(0, 3)
            final_clip.write_videofile(final_path, fps=fps_size)
            final_clip.close()
            main_video.close()
    
            used_path = os.path.join(folder_path, "Used", filename)
            shutil.move(video_path, used_path)