1

用mpg123播放https协议的URL MP3

 1 year ago
source link: https://www.lujun9972.win/linux%E5%92%8C%E5%AE%83%E7%9A%84%E5%B0%8F%E4%BC%99%E4%BC%B4/2016/10/24/%E7%94%A8mpg123%E6%92%AD%E6%94%BEhttps%E5%8D%8F%E8%AE%AE%E7%9A%84url-mp3/%20or%20/linux%E5%92%8C%E5%AE%83%E7%9A%84%E5%B0%8F%E4%BC%99%E4%BC%B4/%E7%94%A8mpg123%E6%92%AD%E6%94%BEhttps%E5%8D%8F%E8%AE%AE%E7%9A%84url-mp3/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

用mpg123播放https协议的URL MP3

mpg123本身是支持URL MP3的,可惜它只支持http协议的url mp3.

好在mpg123还支持从standard input中读取MPEG data,这样以来我们可以通过组合curl与mpg123来实现播放https协议的URL MP3.

#!/bin/bash

# play https mp3 using mpg123

function https_url_p ()
{
    local url=$1
    echo $url |egrep "^https://" >/dev/null
}

uri=$1
if https_url_p $uri;then
    curl -L $uri | mpg123 -
else
    mpg123 $curi
fi

原理很简单:

先判断一个文件是否以 https:// 开头,若是的话,则认为是https url mpg3,那么通过curl将其内容取出来,通过管道发给mpg123来播放.

事实上,curl支持超多的网络协议,你可以很容易的扩展到file,ftp,ftps,rtmp等各种网络协议上.

不过要一个一个判断网络协议的种类就太麻烦了,可以简化成这样:

mpg123 $1 || curl -L $1 |mpg123 -

先尝试用mpg123直接播放,失败的话再听过curl获取MPEG DATA交给mpg123来播放.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK