poke/videobundler/main.py

61 lines
2.2 KiB
Python
Raw Normal View History

import asyncio
import aiohttp
from aiohttp import web
import string
2024-06-22 20:52:41 +02:00
import os
import random
import subprocess
2024-06-23 22:11:12 +02:00
from aiohttp.web import Response, FileResponse
2024-06-22 20:52:41 +02:00
app = web.Application()
app.router._frozen = False
2024-06-22 20:52:41 +02:00
def get_random_string(length):
2024-06-23 00:02:30 +02:00
# choose from all lowercase letter
letters = string.ascii_lowercase
result_str = "".join(random.choice(letters) for i in range(length))
return result_str
async def merge(request):
# register params
job_id = request.rel_url.query["id"]
video_id: str = request.rel_url.query["id"]
audio_itag: str = request.rel_url.query["audio_itag"]
video_itag: str = request.rel_url.query["video_itag"]
# validate
if " " in video_id or len(video_id) > 11:
print(f"Video {video_id} flagged as invalid, dropping request")
return
if not audio_itag.isdigit():
print(f"Audio itag {audio_itag} flagged as invalid, dropping request")
return
if not video_itag.isdigit():
print(f"Video itag {video_itag} flagged as invalid, dropping request")
return
2024-06-23 22:11:12 +02:00
if os.path.isfile(f"{job_id}.mp4"):
return web.FileResponse(
2024-06-23 22:11:12 +02:00
path=f"{job_id}.mp4"
)
2024-06-23 22:11:12 +02:00
cmdline = f"ffmpeg -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={audio_itag}&local=true\" -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={video_itag}&local=true\" -c copy -f mp4 -movflags frag_keyframe+empty_moov {video_id}.mp4"
proc_ffmpeg = await asyncio.create_subprocess_shell(
2024-06-23 22:11:12 +02:00
cmdline,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
2024-06-23 22:11:12 +02:00
print(f"ffmpeg -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={audio_itag}&local=true\" -i \"https://eu-proxy.poketube.fun/latest_version?id={video_id}&itag={video_itag}&local=true\" -c copy -f mp4 -movflags frag_keyframe+empty_moov -")
stdout, stderr = await proc_ffmpeg.communicate()
response = FileResponse(f"{video_id}.mp4")
return response
2024-06-22 20:52:41 +02:00
async def ping(request):
return web.Response(body='{"success": true}', content_type="application/json")
2024-06-22 20:52:41 +02:00
async def init_app():
app.router.add_get("/{id:.+}", merge)
app.router.add_get("/", ping)
return app
2024-06-23 00:02:30 +02:00
if __name__ == '__main__':
loop = asyncio.get_event_loop()
app = loop.run_until_complete(init_app())
web.run_app(app, port=3030)