ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

How to stop ffmpeg.run automatically

Open haima95 opened this issue 2 years ago • 0 comments

when I use udp://233.233.233.223:6666 as ffmpeg.input, How to stop ffmpeg.run automatically where the udp stream is end or destroyed。here my code below,it doesn't work. but if I use rtmp stream , this code works well

import sys import ffmpeg import calendar import time

ts = calendar.timegm(time.gmtime())

input_url = 'udp://233.233.233.223:6666' # 输入为UDP组播流 video_file = 'output_video_{}.mp4'.format(ts) audio_file = 'output_audio_{}.mp3'.format(ts)

构造输入流对象和输出流对象,分别提取音频流和视频流,并保存到文件中

stream = ffmpeg.input(input_url, f='mpegts', rw_timeout=2000000)

视频+音频流输出到mp4文件

out = ffmpeg.output(stream, video_file, c='copy')

分离音频流并输出到mp3文件

out_audio = ffmpeg.output(stream.audio, audio_file, f='mp3')

while True: try: # 运行同时输出视频+音频流和音频流 ffmpeg.run([out, out_audio]) except ffmpeg.Error as e: print("yxj error: ", e.stderr) time.sleep(1)

haima95 avatar May 09 '23 07:05 haima95