我正在尝试编写一个程序,该程序使用 yt-dlp 下载视频的一部分以获取流链接并使用 ffmpeg 下载视频。以下是该程序将执行的操作:
original_link
变量 start_time
和 end_time
分别。 subprocess
,调用shell命令yt-dlp --get-url <original_link>
. video_stream
和 audio_stream
)。 subprocess
下载文件的命令:ffmpeg -ss <start_time> -to <end_time> -i <hls-stream-link> -ss <start_time> -to <end_time> -i <hls-stream-link> output.mkv
* If yt-dlp gives 2 links (Youtube or video-on-demand), then call:
ffmpeg -ss <start_time> -to <end_time> -i <video_stream> -ss <start_time> -to <end_time> -i <audio_stream> output.mkv
我写了一个这样的示例程序:import subprocess
# Input video link
original_link = input("Enter the link of the video: ")
# Input start and end time
start_time = input("Enter video start time: ")
end_time = input("Enter video end time: ")
# Get raw url
stream_link = subprocess.Popen(["yt-dlp", "--get-url", original_link], stdout=subprocess.PIPE)
while True:
line = stream_link.stdout.readline()
if not line:
break
# subprocess.Popen(["ffmpeg", "-ss", start_time, "-to", end_time, "-i"], stdout=subprocess.PIPE)
但我仍然不知道如何引用子进程标准输出中的某些行。例如,如果 yt-dlp 输出两个链接,那么我想让它们传递不同的值(一个用于视频,一个用于音频)。以下是一些示例链接供您使用:感谢您的任何帮助!
最佳答案
使用 subprocess
读取 ffplay 输出的非常简单的示例.
from subprocess import Popen, PIPE, STDOUT
comm_line = ['ffplay','-hide_banner','-autoexit','-i','My.mp4']
try:
play = Popen(comm_line, stdout=PIPE, stderr=STDOUT, universal_newlines=True)
except Exception as e:
print("ffplay", str(e))
play_pid = play.pid
with play.stdout:
for i in iter(play.stdout.readline, b''):
if i != '':
pass
else:
break
print(i)
关于python - 如何在 Python 中引用某个标准输出行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71552296/