首页 > 解决方案 > Python中YouTube视频的ffmpeg输出错误?

问题描述

我不是想下载整个视频,只是 5 秒的视频。我尝试按照本指南进行操作。但是,这是用于行内命令的,我正在尝试使用 python 代码来执行此操作。到目前为止,我已经设法使用 youtube-dl 提取了所有必需的信息。我现在遇到的问题是使用 ffmpeg 输出具有给定参数的视频,但它说它找不到指定的文件。

这是我到目前为止所拥有的:

import youtube_dl
import datetime
import ffmpeg
def getInfo():
    link='https://youtu.be/nda20uSjQUI?t=11981'
    ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})

    #This exctracts all the information of the link
    with ydl:
        result = ydl.extract_info(
            link,
            download=False
        )
    #Grabs the duration of the video
        duration_info = result.get('duration')
    #Grabs the url to the actual video file
        url = str(result).split("'url':",1)[1].split(', ')[0]

    #YouTube(link).streams.first().download('D:/Downloads')
    #Determines the start time based on how the link is formatted
    #They can be in three possible formats
    if 't=' in link:
        if link.endswith('s'):
            time_seconds = link.split('t=', 1)[1].strip('s')
        else:
            time_seconds = link.split('t=', 1)[1]
    else:
        time_seconds = '0'

    #Converts the time into an HH:MM:ss format
    #We only want 5 seconds of video, which is what timestamp_end does
    timestamp_start = str(datetime.timedelta(seconds=int(time_seconds)))
    timestamp_end = str(datetime.timedelta(seconds=int(time_seconds)+5))
    video_length = str(datetime.timedelta(seconds=int(duration_info)))

    #If the segment we want is less than 5 seconds, such as towards the end of the video,
    #then the end will be the end of the actual video
    if timestamp_end > video_length:
        timestamp_end = video_length

    #Returns a list of [url, start, end]
    return url ,timestamp_start,timestamp_end

def download_video(result):
    #Checks to see if there's a valid argument
    if result is None:
        return 'Error: no url found'
    else:
        file_input = ffmpeg.input(result[0])
        out = ffmpeg.concat(file_input.trim(start_frame=result[1],end_frame=result[2])).output('test1.mp4').run()
        return out

download_video(result=getInfo())

这是我得到的错误:

Traceback (most recent call last):
  File "C:/Users/Anonymous/PycharmProjects/untitled/test.py", line 53, in <module>
    download_video(result=getInfo())
  File "C:/Users/Anonymous/PycharmProjects/untitled/test.py", line 50, in download_video
    out = ffmpeg.concat(file_input.trim(start_frame=result[1],end_frame=result[2])).output('test1.mp4').run()
  File "C:\Users\Anonymous\PycharmProjects\untitled3\venv\lib\site-packages\ffmpeg\_run.py", line 320, in run
    overwrite_output=overwrite_output,
  File "C:\Users\Anonymous\PycharmProjects\untitled3\venv\lib\site-packages\ffmpeg\_run.py", line 285, in run_async
    args, stdin=stdin_stream, stdout=stdout_stream, stderr=stderr_stream
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\Lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

Process finished with exit code 1

标签: pythonpython-3.x

解决方案


推荐阅读