Skip to content

Instantly share code, notes, and snippets.

@InJeCTrL
Last active February 17, 2025 15:03
Show Gist options
  • Save InJeCTrL/7ee280e3e7735f736615c5ce1eed0e2f to your computer and use it in GitHub Desktop.
Save InJeCTrL/7ee280e3e7735f736615c5ce1eed0e2f to your computer and use it in GitHub Desktop.
抖音直播录制
// ==UserScript==
// @name Tiktok Live Monitor
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Monitor live status, invoke local record server if onair
// @author InJeCTrL
// @match https://www.douyin.com/user/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=douyin.com
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
setTimeout(function(){ window.location.reload() }, 60000);
var sourceCode = document.documentElement.outerHTML;
if (!sourceCode.includes("\\\"origin\\\":{\\\"main\\\":{\\\"flv\\\":\\\"")) {
localStorage.removeItem("streamId");
return;
}
var flvStart = sourceCode.indexOf("\\\"origin\\\":{\\\"main\\\":{\\\"flv\\\":\\\"") + 32;
var flvUrl = sourceCode.substr(flvStart);
flvUrl = flvUrl.substring(0, flvUrl.indexOf("\\\","));
flvUrl = flvUrl.replaceAll("\\u0026", "&");
console.log(flvUrl);
var streamId = flvUrl.substr(flvUrl.indexOf("stream-"));
streamId = streamId.substring(0, streamId.indexOf(".flv"));
console.log(streamId);
GM_xmlhttpRequest({
url:"http://127.0.0.1:8888/start",
method: "POST",
data: JSON.stringify({"stream_url": flvUrl, "stream_id": streamId}),
headers: {
"Content-Type": "application/json"
}
});
})();
import subprocess
import threading
from flask import Flask, request
from datetime import datetime
app = Flask(__name__)
running_stream_id = set()
map_lock = threading.Lock()
def record(stream_url, stream_id, output_file):
with map_lock:
if stream_id in running_stream_id:
return
running_stream_id.add(stream_id)
subprocess.run([
"ffmpeg",
"-i",
stream_url,
"-c:v",
"copy",
"-c:a",
"copy",
output_file
])
with map_lock:
running_stream_id.remove(stream_id)
@app.route('/start', methods=['POST'])
def start():
json_data = request.get_json()
stream_url = json_data.get('stream_url')
stream_id = json_data.get('stream_id')
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
output_file = "./{}.flv".format(current_time)
thread = threading.Thread(target=record, args=(stream_url, stream_id, output_file))
thread.start()
return {'code': 'ok'}
if __name__ == '__main__':
app.run("127.0.0.1", 8888)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment