如何使用Youtube API v3创建多行描述?

huangapple go评论87阅读模式
英文:

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

问题

以下是您的翻译内容:

我正在使用Python的YouTube API v3上传视频以下是我用于此目的的函数

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)

我像这样调用这个函数

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)

问题是只有"Line 1"实际上会出现在视频描述中我认为`\n`会破坏整个命令因为它将其分为两部分但如何处理它呢

我期望在我的YouTube视频下方获得多行描述
英文:

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.

答案1

得分: 0

你需要使用特殊字符跳过。请尝试使用 \\n 而不是 \n,如下所示:

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)
英文:

You need to utilize special character skipping. Try to use \\n instead of \n as shown below:

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)

答案2

得分: 0

这是一个野生的“猜测”(意思是:我遇到了同样的问题并找到了这个解决方案),根据你的代码设置不同可能不起作用,但对我有效。基本上,我们不想要子进程的东西。相反,我们创建一个函数直接调用上传。所以在你当前的文件中,你可以写类似这样的内容。

from upload_video import upload_video_to_yt

upload_video_to_yt("video/saved/here.mp4", title="视频标题", description="""多行描述""", tags=["是的", "先生"])

在upload_video.py文件的底部添加这个函数。我假设你已经从YouTube网站上复制了大约170行左右的代码。你可以修改这个函数,将类别和隐私状态作为参数传入,这应该相对简单。

def upload_video_to_yt(video_file: str, title: str, description: str, tags: list):
    if not os.path.exists(video_file):
        print("找不到文件:", 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("发生HTTP错误%d\n%s" % (e.resp.status, e.content))
    else:
        print(f"让我们开始吧!文件{video_file}现在已经上传为“{title}”!")

如果有任何问题,请告诉我,或者如果我应该完全删除这个回答。

英文:

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.

huangapple
  • 本文由 发表于 2023年8月4日 04:58:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定