Skip to content

Instantly share code, notes, and snippets.

@fejich
Last active February 14, 2023 06:27
Show Gist options
  • Save fejich/a8e1000a218db1d44cbc92d49bdf68c9 to your computer and use it in GitHub Desktop.
Save fejich/a8e1000a218db1d44cbc92d49bdf68c9 to your computer and use it in GitHub Desktop.
Youtube-DLP 使用记录,配合 emby 使用

项目地址

yt-dlp 是 Youtube-DL 的增强版,更新维护很积极。相比起原版更为稳定,功能更完善。 https://github.com/yt-dlp/yt-dlp

Win 最为方便,直接下载 exe 即可运行,其他系统下还需要系统安装有 Python3.6+ 环境方可运行。

Linux, macOS, BSD 系统均为同一个提供可执行文件

高级功能需要配合 ffmpeg 使用

没有 ffmpeg 情况下,能下载的文件格式有限封面图转换之类的功能也不能工作

还有一些平常用不到的功能需要依赖另外的软件,具体看项目主页

配置文件

于主程序目录下建立 yt-dlp.conf 文件,即会调用。 用来设置输出文件名,代理服务器,下载封面等等非常方便

注意配置文件内不能有中文字符,否则会报错

#filename
-o '%(title)s[%(resolution)s][%(upload_date)s].%(ext)s'

# best HDR or best SDR
#-f 'bestvideo[vcodec=vp9.2]+bestaudio/bestvideo[vcodec=vp9]+bestaudio'

#only m4a
#-f 'ba[ext=m4a]'
#-f wa

#thumbnail to jpg
--write-thumbnail
--convert-thumbnails jpg
#--embed-thumbnail

#proxy
--proxy 127.0.0.1:7890

#other
--write-description
--write-subs
--compat-options no-live-chat
--remux-video mkv

#playlist
#--playlist-items 1-2,11-20

#--skip-download

--exec 'sh /Users/fejich/Movies/Youtube/nfo.sh'

description 文件转为 emby nfo 文件

#!/bin/bash

# 生成临时文件
tee nfo1.temp <<-'EOF'
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<movie>
  <plot><![CDATA[
EOF

tee nfo2.temp <<-'EOF'

]]></plot>
</movie>
EOF

# 生成 emby 可识别的 nfo 文件并添加换行符号
file=$1
filename=${file%.*}
nfo=${filename}.nfo
description=${filename}.description

touch "${nfo}";
cat nfo1.temp > "${nfo}";
sed 's#$#&<br/>#g' "${description}" >> "${nfo}";
cat nfo2.temp >> "${nfo}";

# 删除临时文件
rm nfo1.temp nfo2.temp

description 文件转为 emby nfo 文件(批量)

#!/bin/bash

# 生成临时文件
tee nfo1.temp <<-'EOF'
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<movie>
  <plot><![CDATA[
EOF

tee nfo2.temp <<-'EOF'

]]></plot>
</movie>
EOF

# 批量转换并添加emby识别的换行符号
for file in *.description; do
  nfo=${file%.*}.nfo
  cat nfo1.temp > "${nfo}";
  sed 's#$#&<br/>#g' "${file}" >> "${nfo}";
  cat nfo2.temp >> "${nfo}";
done

# 删除临时文件
rm nfo1.temp nfo2.temp

Win 系统下简单可交互 bat 脚本

@ECHO OFF&PUSHD %~DP0 &TITLE Youtube-DLP 下载脚本
@ echo.
@ echo.
@ echo.
@ echo.
set /p ID= 输入 视频/播放列表网址 按回车:
@ echo.
@ echo.
@ echo.
@ echo.
ECHO 分析下载地址......
yt-dlp %ID%
@ echo.
@ echo.
@ echo.
@ echo.
ECHO 下载完成,任意键退出......
pause>NUL
@YangHgRi
Copy link

YangHgRi commented Aug 3, 2022

你好,大佬,我在windows上安装cygwin然后用你写的sh文件转化.description文件报错了,这是咋回事呢

yt-dlp --downloader aria2c --downloader-args aria2c:"-x 16 -k 1M" --write-thumbnail --convert-thumbnails png --write-description --exec "sh D:\MediaLib\DescriptionToNFO.sh" https://www.youtube.com/watch?v=14K-JEZnuaA
[youtube] 14K-JEZnuaA: Downloading webpage
[youtube] 14K-JEZnuaA: Downloading android player API JSON
[info] 14K-JEZnuaA: Downloading 1 format(s): 313+251
[info] Writing video description to: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].description
[info] Downloading video thumbnail 41 ...
[info] Writing video thumbnail 41 to: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp
[ThumbnailsConvertor] Converting thumbnail "[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp" to png
Deleting original file [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp (pass -k to keep)
[download] Destination: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].f313.webm
[aria2c] Downloaded 643446396 bytes 10MiB]
[download] 100% of 613.64MiB in 01:05
[download] Destination: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].f251.webm
[aria2c] Downloaded 12072161 bytes .1MiB]
[download] 100% of 11.51MiB in 00:11
[Merger] Merging formats into "[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webm"
Deleting original file [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].f313.webm (pass -k to keep)
Deleting original file [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].f251.webm (pass -k to keep)
[Exec] Executing command: sh D:\MediaLib\DescriptionToNFO.sh "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webm"
D:\MediaLib\DescriptionToNFO.sh: line 2: $'\r': command not found

]]>

D:\MediaLib\DescriptionToNFO.sh: line 15: $'\r': command not found
D:\MediaLib\DescriptionToNFO.sh: line 21: $'\r': command not found
touch: cannot touch '"D:'$'\347\273\274\345\220\210\350\265\204\346\226\231\345\272\223'''$'\346\232\202\345\255\230''[4K] '$'\352\260\200\354\243\275'' '$'\353\243\251\353\266\201'' ('$'\354\236\254\354\227\205\353\241\234\353\223\234'')'$'\343\205\243\353\270\224\353\236\231'' & '$'\353\240\210\353\223\234'' '$'\353\260\230\354\247\235\353\260\230\354\247\235'' '$'\352\260\200\354\243\275'' [14K-JEZnuaA]'$'\r''.nfo'$'\r': No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 22: $'\r': command not found
: No such file or directoryO.sh: line 23: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA]
D:\MediaLib\DescriptionToNFO.sh: line 23: $'\r': command not found
: No such file or directoryO.sh: line 24: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA]
D:\MediaLib\DescriptionToNFO.sh: line 24: $'\r': command not found
: No such file or directoryO.sh: line 25: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA]
D:\MediaLib\DescriptionToNFO.sh: line 25: $'\r': command not found
D:\MediaLib\DescriptionToNFO.sh: line 26: $'\r': command not found
rm: cannot remove 'nfo2.temp'$'\r': No such file or directory
ERROR: Postprocessing: Command returned error code 1

@YangHgRi
Copy link

YangHgRi commented Aug 3, 2022

这里是我节选出来的报错的地方,您直接看这个把

[Exec] Executing command: sh D:\MediaLib\DescriptionToNFO.sh "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webm"
D:\MediaLib\DescriptionToNFO.sh: line 2: $'\r': command not found

]]>

D:\MediaLib\DescriptionToNFO.sh: line 15: $'\r': command not found
D:\MediaLib\DescriptionToNFO.sh: line 21: $'\r': command not found
touch: cannot touch '"D:'$'\347\273\274\345\220\210\350\265\204\346\226\231\345\272\223'''$'\346\232\202\345\255\230''[4K] '$'\352\260\200\354\243\275'' '$'\353\243\251\353\266\201'' ('$'\354\236\254\354\227\205\353\241\234\353\223\234'')'$'\343\205\243\353\270\224\353\236\231'' & '$'\353\240\210\353\223\234'' '$'\353\260\230\354\247\235\353\260\230\354\247\235'' '$'\352\260\200\354\243\275'' [14K-JEZnuaA]'$'\r''.nfo'$'\r': No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 22: $'\r': command not found
: No such file or directoryO.sh: line 23: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA]
D:\MediaLib\DescriptionToNFO.sh: line 23: $'\r': command not found
: No such file or directoryO.sh: line 24: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA]
D:\MediaLib\DescriptionToNFO.sh: line 24: $'\r': command not found
: No such file or directoryO.sh: line 25: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA]
D:\MediaLib\DescriptionToNFO.sh: line 25: $'\r': command not found
D:\MediaLib\DescriptionToNFO.sh: line 26: $'\r': command not found
rm: cannot remove 'nfo2.temp'$'\r': No such file or directory
ERROR: Postprocessing: Command returned error code 1

@YangHgRi
Copy link

YangHgRi commented Aug 3, 2022

我谷歌了一下,执行sed -i 's/\r//' DescriptionToNFO.sh把$‘\r‘: command not found的解决方法解决了,但是还是有报错...

PS D:\综合资料库\暂存> yt-dlp --downloader aria2c --downloader-args aria2c:"-x 16 -k 1M" --write-thumbnail --convert-thumbnails png --write-description --exec "sh D:\MediaLib\DescriptionToNFO.sh" https://www.youtube.com/watch?v=14K-JEZnuaA
[youtube] 14K-JEZnuaA: Downloading webpage
[youtube] 14K-JEZnuaA: Downloading android player API JSON
[info] 14K-JEZnuaA: Downloading 1 format(s): 313+251
[info] Writing video description to: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].description
[info] Downloading video thumbnail 41 ...
[info] Writing video thumbnail 41 to: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp
[ThumbnailsConvertor] Converting thumbnail "[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp" to png
Deleting original file [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp (pass -k to keep)
[download] [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webm has already been downloaded
[Exec] Executing command: sh D:\MediaLib\DescriptionToNFO.sh "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webm"

]]>

touch: cannot touch '"D:'$'\347\273\274\345\220\210\350\265\204\346\226\231\345\272\223'''$'\346\232\202\345\255\230''[4K] '$'\352\260\200\354\243\275'' '$'\353\243\251\353\266\201'' ('$'\354\236\254\354\227\205\353\241\234\353\223\234'')'$'\343\205\243\353\270\224\353\236\231'' & '$'\353\240\210\353\223\234'' '$'\353\260\230\354\247\235\353\260\230\354\247\235'' '$'\352\260\200\354\243\275'' [14K-JEZnuaA].nfo': No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 21: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].nfo: No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 22: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].nfo: No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 23: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].nfo: No such file or directory

@fejich
Copy link
Author

fejich commented Aug 7, 2022

我谷歌了一下,执行sed -i 's/\r//' DescriptionToNFO.sh把$‘\r‘: command not found的解决方法解决了,但是还是有报错...

PS D:\综合资料库\暂存> yt-dlp --downloader aria2c --downloader-args aria2c:"-x 16 -k 1M" --write-thumbnail --convert-thumbnails png --write-description --exec "sh D:\MediaLib\DescriptionToNFO.sh" https://www.youtube.com/watch?v=14K-JEZnuaA [youtube] 14K-JEZnuaA: Downloading webpage [youtube] 14K-JEZnuaA: Downloading android player API JSON [info] 14K-JEZnuaA: Downloading 1 format(s): 313+251 [info] Writing video description to: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].description [info] Downloading video thumbnail 41 ... [info] Writing video thumbnail 41 to: [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp [ThumbnailsConvertor] Converting thumbnail "[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp" to png Deleting original file [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webp (pass -k to keep) [download] [4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webm has already been downloaded [Exec] Executing command: sh D:\MediaLib\DescriptionToNFO.sh "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].webm"

]]>

touch: cannot touch '"D:'$'\347\273\274\345\220\210\350\265\204\346\226\231\345\272\223'''$'\346\232\202\345\255\230''[4K] '$'\352\260\200\354\243\275'' '$'\353\243\251\353\266\201'' ('$'\354\236\254\354\227\205\353\241\234\353\223\234'')'$'\343\205\243\353\270\224\353\236\231'' & '$'\353\240\210\353\223\234'' '$'\353\260\230\354\247\235\353\260\230\354\247\235'' '$'\352\260\200\354\243\275'' [14K-JEZnuaA].nfo': No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 21: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].nfo: No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 22: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].nfo: No such file or directory
D:\MediaLib\DescriptionToNFO.sh: line 23: "D:\综合资料库\暂存[4K] 가죽 룩북 (재업로드)ㅣ블랙 & 레드 반짝반짝 가죽 [14K-JEZnuaA].nfo: No such file or directory

cygwin 环境不熟识,你试试使用纯英文目录来运行脚本

还有就是用 wsl 环境来跑 Linux 相关的兼容性应该更好,未来的方向

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment