如果我有一个使用mplayer播放文件的脚本,但我中途停止了回放,有没有办法将回放位置存储在它停止的位置?
发布于 2014-12-14 11:01:55
试试这个,它又快又脏,但在mplayer退出后给了我播放歌曲的秒数
mplayer your.mp3 | tr :cntrl:'\n‘| bbe -e "s/\x0a\x5b\x4a//“| tail -n 4| head -n 1| cut -d ':’-f 2| cut -d '(‘-f 1
发布于 2018-11-15 14:49:59
应该注意的是,这几乎与0800peter's answer做同样的事情,但不需要安装bbe。本质上,这是用一个友好的界面重写了这个答案。此答案还说明了mplayer过早终止的事件(如在pkill mplayer中)。
#!/bin/bash
# desc: Runs mplayer to play input file and returns seconds of playback when stopped
# input:
# arg1: path to audio file
# arg2: pass either [seconds|timestamp]; default timestamp
# output: returns the timestamp or total seconds elapsed when playback stopped
# (ie. when mplayer terminated)
playAudioFile() {
audioFile="$1"
# if you need to modify mplayer switches, do so on the next line
stopPos=$(mplayer "$audioFile" 2> /dev/null | tr [:cntrl:] '\n' | grep -P "A: +\d+\.\d\b" | tail -n1)
# decide what to display
if [ "$2" == "seconds" ]; then
retval=$(awk '{print $2}' <<< "$stopPos")
else
retval=$(awk '{print $3}' <<< "$stopPos" | tr -d '()')
fi
echo "$retval"
}
#example usage
path="$1"
stopPosition=$(playAudioFile "$path")
echo "$stopPosition"我的脚本接受音频文件的路径,当mplayer终止(正常或异常)时,会返回一个时间戳或秒数。如果您选择接收时间戳,请注意,对于任何值为零的单位,时间戳都不会有占位符。也就是说,00:00:07.3将作为07.3返回,00:10:01.2将作为10:01.2返回
如果我想把mplayer发送到后台怎么办?
如果您希望能够启动mplayer并将其发送到后台,并且仍然能够查询这些信息,那么您可能需要查看用于跟踪播放状态信息的bash script I wrote。该脚本包含两个名为getElapsedTimestamp和getElapsedSeconds的函数,即使mplayer已经终止,这两个函数也会返回播放时间。要使用这些功能,必须使用我的playMediaFile函数启动媒体文件。这个函数可以像这样调用来启动mplayer并发送到后台...
playMediaFile "path/to/your/file" &https://stackoverflow.com/questions/13432612
复制相似问题