Skip to content

Commit 8fb715d

Browse files
use ffmpeg to transcode audio
1 parent 1641e08 commit 8fb715d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

beetsplug/beetstream/songs.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from beetsplug.beetstream.utils import *
2-
from beetsplug.beetstream.stream import stream
3-
from beetsplug.beetstream import app
2+
from beetsplug.beetstream import app, stream
43
from flask import g, request, Response
54
from beets.random import random_objs
65
import xml.etree.cElementTree as ET
@@ -54,15 +53,18 @@ def stream_song():
5453
id = int(song_subid_to_beetid(request.values.get('id')))
5554
item = g.lib.get_item(id)
5655

57-
return stream(item.path.decode('utf-8'), maxBitrate)
56+
if maxBitrate > 0 and item.bitrate > maxBitrate * 1000:
57+
return stream.try_to_transcode(item.path.decode('utf-8'), maxBitrate)
58+
else:
59+
return stream.send_raw_file(item.path.decode('utf-8'))
5860

5961
@app.route('/rest/download', methods=["GET", "POST"])
6062
@app.route('/rest/download.view', methods=["GET", "POST"])
6163
def download_song():
6264
id = int(song_subid_to_beetid(request.values.get('id')))
6365
item = g.lib.get_item(id)
6466

65-
return stream(item.path.decode('utf-8'), 0)
67+
return stream.send_raw_file(item.path.decode('utf-8'))
6668

6769
@app.route('/rest/getRandomSongs', methods=["GET", "POST"])
6870
@app.route('/rest/getRandomSongs.view', methods=["GET", "POST"])

beetsplug/beetstream/stream.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
from beetsplug.beetstream.utils import path_to_content_type
22
from flask import send_file, Response
33

4+
import ffmpeg
5+
46
def send_raw_file(filePath):
57
return send_file(filePath, mimetype=path_to_content_type(filePath))
68

7-
def stream(filePath, maxBitrate):
8-
return send_raw_file(filePath)
9+
def transcode_and_stream(filePath, maxBitrate):
10+
outputStream = (
11+
ffmpeg
12+
.input(filePath)
13+
.output('pipe:', format="mp3", audio_bitrate=maxBitrate * 1000)
14+
.run_async(pipe_stdout=True)
15+
)
16+
17+
return Response(outputStream.stdout, mimetype='audio/mpeg')
18+
19+
def try_to_transcode(filePath, maxBitrate):
20+
return transcode_and_stream(filePath, maxBitrate)

0 commit comments

Comments
 (0)