2014年10月20日 星期一

使用mplayer播放影片

安裝mplayer
使用pacman -S mplayer安裝mplayer時遇到錯誤:
error: failed retrieving file 'lirc-utils-1:0.9.1.a-3-x86_64.pkg.tar.xz' from mirrors.ustc.edu.cn : The requested URL returned error: 404 Not Found
解法:
上 http://ftp.seblu.net/archlinux/arm/packages/l/libx264/ 下載tar.xz檔然後手動安裝
# pacman -U lirc-utils-1:0.9.1.a-3-x86_64.pkg.tar.xz
再安裝mplayer
又出現錯誤:
error: failed retrieving file 'libx264-1:142.20140826-1-x86_64.pkg.tar.xz' from mirrors.ustc.edu.cn : The requested URL returned error: 404 Not Found
同理,一樣上網下載package檔手動安裝,直到使用pacman -S mplayer安裝mplayer成功為止

使用API在web server 播放影片
web server 連結自己的螢幕,使用者透過網頁想要用ajax去播web server 的影片在server的螢幕上
出現錯誤:
...
Terminal type `unknown' is not defined. Playing /srv/http/codeigniter/videos/3.mp4.
...
PHP:
system('bash play_muti.sh');

play_muti.sh:
#!/bin/bash
mplayer -vo xv -noborder -zoom -ontop -x 512 -y 384 -geometry 0:0 mf://\/srv/http/codeigniter/videos/Koala.jpg -loop 0 & 
mplayer -vo xv -noborder -zoom -ontop -x 512 -y 384 -geometry 512:0 /srv/http/codeigniter/videos/1.mp4 &

註:
在console中,登錄桌面,執行play_muti.sh( # bash play_muti.sh )可以正常播放
原因:
因為點瀏覽器的api,主機上是用apache執行,所以要讓apache能以桌面登錄的使用者(ex. bear)播放影片
解法:
1. 編輯 /etc/suders
加入這行:
http ALL=(ALL) NOPASSWD: ALL #讓apache可以以別人帳號執行命令
2. 修改play_muit.sh,在前面加上sudo -u <username>:( http://stackoverflow.com/questions/6243327/how-to-run-mplayer-with-audio-speaker-output-from-php-web-script-on-linux )
#!/bin/bash
sudo -u bear mplayer -vo xv -noborder -zoom -ontop -x 512 -y 384 -geometry 0:0 mf://\/srv/http/codeigniter/videos/Koala.jpg -loop 0 & # 前面加上 sudo -u bear ,以bear執行mplayer
sudo -u bear mplayer -vo xv -noborder -zoom -ontop -x 512 -y 384 -geometry 512:0 /srv/http/codeigniter/videos/1.mp4 &

3. php在system()前面加入 putenv("DISPLAY=:0.0"); ( http://unix.stackexchange.com/questions/14408/running-mplayer-through-a-php-script )

執行api (ex. http://domain/play.php )即可在web server播放影片和圖片(注意:不是播在使用者的瀏覽器上)

播放完後影片不要關掉
mplayer參數加上-idle -fixed-vo

使用quiet和slave模式
$ mplayer -quiet -slave 3.mp4
MPlayer SVN-r37224 (C) 2000-2014 MPlayer Team
...
pause/stop (可在終端上輸入pause暫停影片、stop關掉影片)
get_time_pos (抓取現在播到第幾秒,可以判斷是否播完)
ANS_TIME_POSITION=5.2

用Makefile+fifo做:
$ mkfifo fifofile
$ mkfifo fifofile2
Makefile:
BIN=mplayer
SCR=:0
MOV=4.mp4
MOV2=3.mp4
FIFO=/your_vidoes_path/videos/fifofile
FIFO2=/your_videos_path/videos/fifofile2

all:
    nohup $(BIN) -noborder -zoom -ontop -x 512 -y 384 -geometry 0:0 -slave -input file=$(FIFO) $(MOV) 2>&1 1>player.log &
    nohup $(BIN) -noborder -zoom -ontop -x 512 -y 384 -geometry 512:0 -slave -input file=$(FIFO2) $(MOV2) 2>&1 1>player.log &

pause:
    echo "pause" > $(FIFO) &
    echo "pause" > $(FIFO2) &

clear:
    killall $(BIN)

播影片:
$ make
暫停:
$ make pause
結束:
$ make clear

不建議在shell script使用killall mplayer殺掉mplayer程式再跑後面的mplayer,因為後面的mplayer有機會開不起來,請使用:
$ ps aux | grep mplayer | awk '{print $2}' | xargs kill

循環播放影片
http://go2linux.garron.me/mplayer-command-line-music-video-player
-loop 0 1.mp4 2.mp4 3.mp4 ...

取得影片長度
http://superuser.com/questions/650291/how-to-get-video-duration-in-seconds
$ ffmpeg -i file.flv 2>&1 | grep "Duration"| cut -d ' ' -f 4 | sed s/,// | sed 's@\..*@@g' | awk '{ split($1, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] }' 2383

相關文章:
http://carlislebear.blogspot.tw/2014/11/arch-slim-failed-to-execute-login-command.html

沒有留言:

張貼留言