将 R-Pi 打造成一个大屏幕的服务器
R-Pi 的 hdmi 输出,用来看视频是件不错的事情。试曾想过我们在手机或者平板上看到一张美丽的图片,视频,然后想在高清电视,上播放出来。
有个很简单的方法。在 R-Pi 上开启 http 服务,通过局域网,由手机或者平板,提交信息给 R-Pi然后就可以了。
下面以网络视频为例:
1、首先安装一些基本的软件和软件包:
# apt-get install mplayer
# easy_install bottle
# easy_install beautifulsoup4
# easy_install requests
# easy_install mplayer.py2、导入基本的模块。
from bottle import Bottle, request, response
import mplayer, requests
from bs4 import BeautifulSoup
import os, re
import json
3、初始化:
app = Bottle()
player = None
4、解析视频地址:
def parse_video(url, format='nomal'):
browser = requests.session()
browser.config['base_headers']['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11'
r = browser.get('http://www.flvcd.com/parse.php?kw=%s&flag=one&format=%s'%(url, format))
r.encoding = 'GBK'
html = BeautifulSoup(r.text)
mform = html.find('form', {'name':'mform', 'method':'post'})
if mform:
name = mform.find('input', {'name':'name'}).get('value')
info = mform.find('input', {'name':'inf'}).get('value')
urls =
return urls
return None
5、播放视频:
@app.post('/play')
def play():
global player
data = request.forms.get('json', None)
if data is None:
return {'err':'403', 'msg':'request error'}
print(data)
data = json.loads(data)
files = data.get('files', [])
url = data.get('url')
last = data.get('start', 0)
fullscreen = data.get('fullscreen', False)
volume = data.get('volume')
if url:
format = data.get('format', 'nomal')
files = parse_video(url, format)
if files is None or len(files)==0:
return {'err':'403', 'msg':'request error'}
if player is None or not player.is_alive():
player = mplayer.Player()
if fullscreen:
player.fullscreen = bool(fullscreen)
if volume:
player.volume = float(volume)
for fn in files:
player.loadfile(fn, last)
last += 1
return {'last': last}
6、暂停:
@app.post('/pause')
def pause():
if player and player.is_alive():
player.pause()
return {'paused':player.paused}
else:
return {'err':'403', 'msg':'player is quit'}
@app.get('/paused')
def paused():
if player and player.is_alive():
return {'paused':player.paused}
else:
return {'err':'403', 'msg':'player is quit'}
7、退出
@app.post('/quit')def quit():
global player
if player and player.is_alive():
player.quit()
player = None
return {'msg':'quit'}
8、全屏
@app.post('/fullscreen')
def fullscreen(is_set=True):
if player and player.is_alive():
if is_set:
player.fullscreen = not player.fullscreen
return {'fullscreen':player.fullscreen}
return {'err':'403', 'msg':'player is quit'}
@app.get('/fullscreen')
def _fullscreen():
return fullscreen(False)
9、声音
@app.post('/volume')
def volume(is_set=True):
if player and player.is_alive():
if is_set:
volume = request.forms.get('value', 100.0)
player.volume = float(volume)
return {'volume':player.volume}
return {'err':'403', 'msg':'player is quit'}
@app.get('/volume')
def _volume():
return volume(False)
10、定位@app.post('/seek')
def seek():
if player and player.is_alive():
value = request.forms.get('value', 0)
type = request.forms.get('type', 0)
player.seek(float(value), int(type))
return {'length':player.length, 'position':player.percent_pos}
return {'err':'403', 'msg':'player is quit'}
@app.post('/pt_step')
def pt_step():
if player and player.is_alive():
value = request.forms.get('value', 1)
force = request.forms.get('force', 0)
if force:
player.pt_step(int(value), int(force))
else:
player.pt_step(int(value))
return {'filename':player.filename}
else:
return {'err':'403', 'msg':'player is quit'}
11、状态@app.get('/status')
def status():
if player and player.is_alive():
return {'filename':player.filename, 'position':player.percent_pos, 'length':player.length}
else:
return {'err':'403', 'msg':'player is quit'}
12、静音@app.post('/mute')
def mute():
if player and player.is_alive():
player.mute = not player.mute
return {'mute':player.mute}
else:
return {'err':'403', 'msg':'player is quit'}
13、mainif __name__ == '__main__':
from bottle import run
run(app)14、将以上代码保存为 app.py
15、运行:
$ python app.py 播放视频:
import requests
import json
def api(uri):
return 'http://127.0.0.1:8080' + uri
data = {
'url':'http://v.youku.com/v_show/id_XNDU3NjM0NzMy.html',# require
'files', [], # require
'start':0, # 0 为立即播放
'format': 'nomal', # nomal, super
'fullscreen': True,
'volume': 90
} # files/url, 必须有一个,url > files
r = requests.post(api('/play'), data={'json':json.dumps(data)})
print(r.json)
查看是否全屏:
r = requests.get(api('/fullscreen'))
print(r.json)
改变全屏状态:
r = requests.post(api('/fullscreen'))
print(r.json)
查看音量:
r = requests.get(api('/volume'))
print(r.json)
修改音量:
r = requests.post(api('/volume'), data={'value':50})
print(r.json)
查看是否暂停:
r = requests.get(api('/paused'))
print(r.json)
修改暂停状态:
r = requests.post(api('/pause'))
print(r.json)
定位:
r = requests.post(api('/seek'), data={
'value':20,
'type':0 # 可选,0 是一个相对定位+/- <value>(默认值)。
# 1 是定位<value>%在电影里。
# 2 是寻求一个绝对位置的<value>秒。
})
print(r.json)
下一个/上一个视频:
r = requests.post(api('/pt_step'), data={
'value':1 # 1 为下一个,-1 上一个
'force':0 # 可选 默认为 0
})
print(r.json)
静音:
r = requests.post(api('/mute'))
print(r.json)
查看状态:
r = requests.get(api('/status'))
print(r.json)
退出:
r = requests.post(api('/quit'))
print(r.json) 倒时候开发个 手机客户端,这样就一个真正的大屏幕啦! 好深奥 支持一下! 强人啊,好帖必须顶 有没有C语言版本的,没学过python的路过帮顶 没图不够爽啊 不知道兄台有没有测试过..mplayer在pi上根本不能用的
通用播放器不支持硬件加速的话..解码速度在3-4FPS左右.. 抓youjizz.com 的数据 实时更新小 电影 没看懂楼主要干嘛