我正在编写一个程序,下载视频的一部分,使用yt获取流链接,ffmpeg下载视频。下面是这个项目的作用:
respectively.
original_link
变量
start_time
和end_time
subprocess
,调用shell命令yt-dlp --get-url <original_link>
.
audio_stream
).
video_stream
),我们将调用另一个subprocess
命令来下载文件:如果yt-dlp只提供1链接,则执行:(正如我前面提到的,这是用于视频和音频input)的单个HLS流链接)。
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输出两个链接,那么我希望让它们以单独的值传递(一个用于视频,另一个用于音频)。下面是一些供您使用的示例链接:
谢谢你的帮助!
发布于 2022-03-21 10:53:48
一个非常简单的用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)
https://stackoverflow.com/questions/71552296
复制相似问题