在下载使用YouTube-DL和FFMPEG库下载修剪的YouTube视频的音频数据时面临问题

发布于 2025-01-29 02:08:45 字数 908 浏览 4 评论 0原文

我正在尝试使用youtube-dl(v2021.12.17)和ffmpeg(v4.2.2)python上的YouTube视频的音频内容下载YouTube视频的音频内容,在Windows 10 PC上。当在Pycharm终端执行命令时,我可以下载音频数据,但是在使用OS.System()命令时无法下载相同的内容。

执行命令时,我可以下载数据

"ffmpeg -ss 30 -t 10 -i $(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0) -- output1.wav"

例如,当我在Pycharm终端

。但是,我遇到了一个错误,当我尝试在编辑器窗口中执行相同的操作,如下所示,

os.system('ffmpeg -ss 30 -t 10 -i $(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0) -- output1.wav')

error: $(youtube-dl: No such file or directory

我还尝试将$()链接封闭在引号中,然后执行。但是,在执行此操作之后,我遇到了不同的错误,如下所示

os.system('ffmpeg -ss 30 -t 10 -i "$(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0)" -- output1.wav')

error: $(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0): Invalid argument

,你们中的任何一个人都可以帮助我解决此错误吗?

I'm trying to download the audio content of a trimmed YouTube video using youtube-dl (v2021.12.17) and FFmpeg (v4.2.2) libraries in python, on Windows 10 PC. I am able to download the audio data when the command is executed on the Pycharm terminal, but not able to download the same content when using os.system() command.

For example, I'm able to download the data when I execute

"ffmpeg -ss 30 -t 10 -i $(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0) -- output1.wav"

command on the Pycharm terminal.

But, i'm getting an error, when I try to execute the same in the Editor window as shown below,

os.system('ffmpeg -ss 30 -t 10 -i $(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0) -- output1.wav')

error: $(youtube-dl: No such file or directory

I also tried enclosing the $() link in quotes and then executed it. But, after doing this, I am getting a different error as shown below

os.system('ffmpeg -ss 30 -t 10 -i "$(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0)" -- output1.wav')

error: $(youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0): Invalid argument

So, can any of you help me in resolving this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2025-02-05 02:08:45

它不起作用,因为您的命令实际上是一个shell脚本,通过$(...)从另一个系统调用中插入字符串。 Python的OS.System()一次只能运行一个shell命令(基于观察)。

这是什么意思?您需要运行YouTube-DLffmpegsubprocess分别命令:

import subprocess as sp

# run youtube-dl and capture its output as a string
results = sp.run(['youtube-dl','-f','best','-g',
                  'https://www.youtube.com/watch?v=3NGZcpAZcl0'],
                 stdout=sp.PIPE,universal_newlines=True)
url = results.stdout

# run ffmpeg
sp.run(['ffmpeg','-ss','30','-t','10','-i', url, 'output1.wav')

It doesn't work because your command is actually a shell script inserting a string from another system call by $(...). Python's os.system() can run only one shell command at a time (based on observation).

What does this mean? You need to run youtube-dl and ffmpeg commands separately with subprocess:

import subprocess as sp

# run youtube-dl and capture its output as a string
results = sp.run(['youtube-dl','-f','best','-g',
                  'https://www.youtube.com/watch?v=3NGZcpAZcl0'],
                 stdout=sp.PIPE,universal_newlines=True)
url = results.stdout

# run ffmpeg
sp.run(['ffmpeg','-ss','30','-t','10','-i', url, 'output1.wav')
沉睡月亮 2025-02-05 02:08:45

我怀疑这是一个路径问题。
尝试指定YouTube-DL的完整路径:

os.system('ffmpeg -ss 30 -t 10 -i $(/usr/local/bin/youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0) -- output1.wav')

I suspect it's a PATH problem.
Try specifying the full path of youtube-dl:

os.system('ffmpeg -ss 30 -t 10 -i $(/usr/local/bin/youtube-dl -f best -g https://www.youtube.com/watch?v=3NGZcpAZcl0) -- output1.wav')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文