使用VLC库从实时流中保存音频 [英] Saving audio from livestream using VLC library

查看:64
本文介绍了使用VLC库从实时流中保存音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VLC库从实时流中保存音频剪辑(每个剪辑15秒).我找不到任何可以让我仅录制15秒直播内容的选项.因此,我最终在代码中使用了timer,但是录制剪辑有时包含10秒,有时包含20秒(很少是15秒).同样,有时音频内容会在剪辑中重复出现.这是代码(我是新手,请指导我)

I am trying to save audio clips (15 seconds per clip) from live stream using VLC library. I am unable to find any option that could allow me to record only 15 seconds from the live stream. Thus I ended up using timer in my code, but the recording clips sometimes contain 10 seconds, sometimes 20 seconds (rarely 15 seconds). Also, sometimes the audio content is repeated in the clips. Here is the code (I am a newbie so please guide me)

Code.py

import os
import sys
import vlc
import time

clipNumber = sys.argv[1]

filepath = 'http://streamer64.eboundservices.com/geo/geonews_abr/playlist.m3u8'
movie = os.path.expanduser(filepath)
if 'http://' not in filepath:
    if not os.access(movie, os.R_OK):
        print ( 'Error: %s file is not readable' % movie )
        sys.exit(1)
filename_and_command = "--sout=#transcode{vcodec=none,acodec=mp3,ab=320,channels=2,samplerate=44100}:file{dst=clip" + str(clipNumber) + ".mp3}"
#    filename_and_command = "--sout=file/ts:clip" + str(clipNumber) + ".mp3"
instance = vlc.Instance(filename_and_command)
try:
    media = instance.media_new(movie)
except NameError:
    print ('NameError: % (%s vs Libvlc %s)' % (sys.exc_info()[1],
                    vlc.__version__, vlc.libvlc_get_version()))
    sys.exit(1)
player = instance.media_player_new()
player.set_media(media)
player.play()
time.sleep(15)
exit()

现在我想录制1分钟的直播,我从bash脚本调用了python代码4次,它创建了4个音频剪辑(clip1.mp3,clip2.mp3,clip3.mp3和clip4.mp3)

Now that I want to record 1 minute of the live-stream, I invoke this python code from the bash script 4 times and it creates 4 audio clips (clip1.mp3, clip2.mp3, clip3.mp3 and clip4.mp3)

Script.sh

for ((i=1; i<=4; i++))
do
    printf "Recording stream #%d\n", "$i"
    python code.py "$i"
    printf "Finished stream #%d\n", "$i"
done

无论如何,是否仅使用Python循环代码,而不是使用bash脚本一次又一次地调用代码(我试图将代码放入python循环中,但是第一个剪辑-clip1-保持记录并且从未完成记录).一种指定我只能从直播中录制15秒的方法,而不是使用time.sleep(15)

Is there anyway to just loop the code with Python instead of invoking again and again with bash script (I tried to put the code in the loop in python, but the first clip - clip1 - keeps recording and never finishes recording). And a way to specify that I could only record 15 seconds from the live-stream instead of using time.sleep(15)

推荐答案

使用FFMPEG可以轻松完成所有所需的工作,如下:

All of the work required can easily be done with FFMPEG as:

ffmpeg -i streamURL -c copy -vn -ac 2 -acodec aac -t 15

-vn 仅用于录制音频部分(不包括视频)

-vn for just recording the audio part (without video)

-t 用于指定要记录的流的持续时间(此处为15秒)

-t for specifying the duration of stream you want to record (15 sec here)

这篇关于使用VLC库从实时流中保存音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆