Created
June 6, 2020 15:07
-
-
Save yoavst/9d6cb54eb47737b744bbe204e4c4a6e5 to your computer and use it in GitHub Desktop.
Radio recorder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function descriptor() | |
return { | |
title = "Radio recorder", | |
version = "0.1", | |
author = "Yoav", | |
url = "", | |
shortdesc = "Radio recorder", | |
description = "Helps record the radio", | |
capabilities = { "input-listener" } | |
} | |
end | |
-- Activate, Close & Deactivate | |
function activate() | |
vlc.msg.dbg("[VLC Playing] Activate!") | |
update_track() | |
end | |
function close() | |
vlc.msg.dbg("[VLC Playing] Close!") | |
end | |
function deactivate() | |
vlc.msg.dbg("[VLC Playing] Deactivate!") | |
end | |
-- Triggers | |
function input_changed() | |
vlc.msg.dbg("[VLC Playing] Input Change!") | |
update_track() | |
end | |
function meta_changed() | |
vlc.msg.dbg("[VLC Playing] Meta Change!") | |
update_track() | |
end | |
-- Update Track | |
function update_track() | |
vlc.msg.dbg("[VLC Playing] Update Track!") | |
if vlc.input.is_playing() then | |
local item = vlc.item or vlc.input.item() | |
if item then | |
local meta = item:metas() | |
if meta then | |
local uri = item:uri() | |
local name = meta["now_playing"] | |
if not name then | |
name = "Unknown name" | |
end | |
send_data(name, uri) | |
return true | |
end | |
end | |
end | |
end | |
function send_data(name, uri) | |
local u = vlc.net.url_parse("http://localhost:8000/") | |
local host, port, path = u["host"], u["port"], u["path"] | |
local header = { | |
"GET "..path.." HTTP/1.1", | |
"Host: "..host, | |
"url: "..uri, | |
"name: "..name, | |
"", | |
"" | |
} | |
local request = table.concat(header, "\r\n") | |
http_req(host, port, request) | |
end | |
function http_req(host, port, request) | |
local fd = vlc.net.connect_tcp(host, port) | |
if not fd then return false end | |
local pollfds = {} | |
pollfds[fd] = vlc.net.POLLIN | |
vlc.net.send(fd, request) | |
vlc.net.poll(pollfds) | |
local chunk = vlc.net.recv(fd, 2048) | |
while chunk do | |
vlc.net.poll(pollfds) | |
chunk = vlc.net.recv(fd, 1024) | |
end | |
vlc.net.close(fd) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
from http.server import HTTPServer, BaseHTTPRequestHandler | |
FILENAME = 'currentRecording.mp3' | |
VLC_PATH = "C:\\Program Files (x86)\\VideoLAN\VLC\\vlc.exe" | |
old_recording = None | |
old_name = None | |
should_save = False | |
def saved_name(name): | |
return "recordings\\" + name.replace(" ", "_") + ".mp3" | |
def generate_vlc_record_cmd(url, filename): | |
return [VLC_PATH, "-vvv", url, "-I", "dummy", "--sout", "#transcode{acodec=mp3,ab=128}:file{dst=" + filename + "}"] | |
def on_new_media(url, name): | |
global old_recording, should_save, old_name | |
if old_recording: | |
print("Finished old recording") | |
old_recording.terminate() | |
old_recording.wait() | |
if should_save: | |
print("Saving old recording") | |
os.rename(FILENAME, saved_name(old_name)) | |
should_save = False | |
old_recording = subprocess.Popen(generate_vlc_record_cmd(url, FILENAME)) | |
old_name = name | |
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
global should_save | |
self.send_response(200) | |
self.end_headers() | |
if self.path == "/": | |
try: | |
stream_url = self.headers['url'] | |
current_name = self.headers['name'] | |
print("Received new media:", stream_url, current_name) | |
on_new_media(stream_url, current_name) | |
except KeyError: | |
print("problem with http request") | |
elif self.path == "/save": | |
print("Received a save request") | |
should_save = True | |
httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why are there no instructions for use? This kind of stuff is gobbledygook words on a page if you don't explain it.