Adding new line in video description via YouTube D

2019-06-07 08:30发布

I am using https://github.com/tokland/youtube-upload to upload videos, however, I am not able to break a line in the video description. Any way this could be done?

For example, I am successful with:

C:\Python35\youtube-upload-master\bin>youtube-upload --title="Title" \
--description="This is one line" --category=Music "video.mp4"

I have tried adding escape sequences \\n, \n, \r\n in the middle of description but they are added without escaping (e.g. This is \\n one line). Also tried <br /> but HTML tags are not allowed.

[RequestError] Server response: {
 "error": {
  "errors": [
   {
    "domain": "youtube.video",
    "reason": "invalidDescription",
    "message": "The request metadata specifies an invalid video description.",
    "locationType": "other",
    "location": "body.snippet.description"
   }
  ],
  "code": 400,
  "message": "The request metadata specifies an invalid video description."
 }
}

3条回答
戒情不戒烟
2楼-- · 2019-06-07 08:55

I have overcome this issue by modifying the source file (https://github.com/tokland/youtube-upload/blob/master/youtube_upload/main.py). Particular place was lines 103-106:

if hasattr(u('string'), 'decode'):   
    description = u(options.description or "").decode("string-escape")
else:
    description = options.description

Looks like decoding removed escaped characters, thus no new lines were created.

I have replaced these with:

description = description.replace('#n', '\r\n')

I will use this special character (#n) sequence to resemble a new line beginning. This sequence I will need to include in the description text when doing the pre-porcessing of it.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-06-07 09:05

\\n is correct and is working, there is no need for any special tricks

查看更多
Summer. ? 凉城
4楼-- · 2019-06-07 09:08

This took me a long time to figure out and so I thought I'd help some people out here. The reason that this is working for some people and not working for others, I am almost positive, has to do with the way that you're actually passing the data into the Python script.

Most people who found the upload_video.py file are doing so from the command line. Unfortunately when the description data is passed into Python it changes the \n to a literal slash and 'n' instead of a newline.

What the responder did above was to change the newline character in his Bash input to #n. I went ahead and used \n. It can be changed to anything you'd like it to be provided you are consistent with the modification within the Python script.

If you edit upload_video.py and around line 94 add in the following:

formattedDescription = options.description.replace( '\\n', '\n' )

A few lines down you'll also need to edit this section:

  title=options.title,
  description=options.description,
  tags=tags,
  categoryId=options.category

So that it reads:

  title=options.title,
  description=formattedDescription,
  tags=tags,
  categoryId=options.category

The reason I've created a separate variable is so that I can dump it to the screen and ensure the newlines are being properly displayed.

Anyway, it was a very simple modification; I tested it thoroughly, it passes through the YouTube API perfectly. Even if you want to try your own method, just dump the description variable out and you'll quickly see why the \n isn't otherwise working. YouTube DOES accept the \n in the description; your script just isn't passing it.

查看更多
登录 后发表回答