Search code examples
pythonyoutubeyoutube-api

How to create a multi-line description using Youtube API v3?


I am uploading videos with Python Youtube API v3. Below is my function which I use for it:

def YTUpload(file, title, description, keywords, category, privacyStatus):
    command = [
        'python',
        'upload_video.py',
        f'--file="{file}"',
        f'--title="{title}"',
        f'--description="{description}"',
        f'--keywords="{keywords}"',
        f'--category="{category}"',
        f'--privacyStatus="{privacyStatus}"',
        '--noauth_local_webserver'
    ]
    subprocess.run(' '.join(command), shell=True)

I call this function like this:

if render_spedup:
    video = "Output/Video.mp4"
    title = "Title"
    keywords = "keywords"
    description = f"Line 1\nLine 2"
    
    try:
        YTUpload(video, title, description, keywords, category, privacyStatus)
    except Exception as err:
        print(err)

The problem is that only "Line 1" will actually be in the video description. I think that \n breaks the whole command because it separates it in two, but how to deal with it?

I am expecting to get multi-line description below my Youtube video.


Solution

  • This is a wild "guess" (read: I had the same issue and found this solution) and may not work depending on how your code is set up, but it worked for me. Basically, we don't want the subprocess thing. Instead, we make a function to invoke the upload directly. So in your current file you write something like this.

    from upload_video import upload_video_to_yt
    
    upload_video_to_yt("video/saved/here.mp4", title="Title of video", description="""
    Multi
    
    Line
    
    Description
    """, tags=["yes", "sir"])
    

    In the upload_video.py file you add this function to the bottom. I am assuming you already have approx 170 lines of code or so taken from the YouTube website. You can modify this function to take in the category and privacy status as arguments, which should be fairly straightforward.

    def upload_video_to_yt(video_file: str, title: str, description: str, tags: list):
        if not os.path.exists(video_file):
            print("Couldn't find file:", video_file)
            exit(1)
    
        information = Namespace(
            auth_host_name="localhost",
            noauth_local_webserver=False,
            auth_host_port=[8080, 8090],
            logging_level="INFO",
            file=video_file,
            title=title,
            description=description,
            category="24",
            keywords=tags,
            privacyStatus="private",
        )
    
        youtube = get_authenticated_service(information)
        try:
            initialize_upload(youtube, information)
        except HttpError as e:
            print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
        else:
            print(f"LETS GO! FILE {video_file} IS NOW UPLOADED AS \"{title}\"!")
    

    Let me know if there is anything that doesn't work, or if I should just delete this answer entirely.