python-vlc
python-vlc copied to clipboard
Using python-vlc to save livestream to disk, How to?
I am using the following code to save a live stream to disk for over a week's worth. In this case it's the NYC Times sq. stream. This code runs OK but disconnects in a few seconds. I want to save the stream for extended periods of time (say 7-10 days). How can I go about it with python-vlc ? Or should I be using ffmpeg?
Sorry I am a newbie to this.
youtube_video_url = "https://www.youtube.com/watch?v=eJ7ZkQ5TC08"
instance = vlc.Instance()
player = instance.media_player_new()
dl_dir = 'streams'
try:
video = pafy.new(youtube_video_url)
best = video.getbest(preftype="mp4")
filename = best.filename
tgt_folder = os.path.join(dl_dir,filename.replace(".mp4",""))
if not os.path.isdir(tgt_folder): os.makedirs(tgt_folder)
print("[*]", filename)
playurl = best.url
media = instance.media_new(playurl)
media.get_mrl()
player.set_media(media)
filepath = os.path.join(tgt_folder,datetime.now().strftime('%Y-%m-%d %H-%M-%S.mp4'))
player.play
best.download(quiet=False,filepath=filepath)
except ValueError as e:
print("[!]", e)
You need to set the corresponding options for saving a stream with libVLC. For the required command, see here.
Something similar to:
save_file_option = f'--sout="#std{{access=file,mux=mp4,dst={filepath}}}"'
media = instance.media_new(playurl, save_file_option)