Skip to content

Instantly share code, notes, and snippets.

@jcardus
Created March 17, 2025 23:49
Show Gist options
  • Save jcardus/f027bc6fb2e4d99257d638a3467542bd to your computer and use it in GitHub Desktop.
Save jcardus/f027bc6fb2e4d99257d638a3467542bd to your computer and use it in GitHub Desktop.
convert h.265
import os
import subprocess
import requests
from flask import Flask, request, send_file, jsonify
import base64
TRACCAR_SERVER_URL = "."
app = Flask(__name__)
@app.route('/api/media/<path:media_path>', methods=['GET'])
def convert_video(media_path):
print(media_path)
auth_header = request.args.get('aJSESSIONID')
media_url = f"{TRACCAR_SERVER_URL}/api/media/{media_path}"
ffmpeg_cmd = [
"ffmpeg", "-headers", f"Cookie: JSESSIONID={auth_header}",
"-i", media_url, "-c:v", "libx264", "-preset", "fast",
"-crf", "23", "-c:a", "aac", "-b:a", "128k", "-f", "matroska", "pipe:1"
]
try:
print(auth_header)
print("FFmpeg command:", " ".join(ffmpeg_cmd))
result = subprocess.run(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Capture FFmpeg logs (stderr)
ffmpeg_logs = result.stderr.decode('utf-8')
# Optionally, you can log it to a file or print it for debugging
print("FFmpeg logs:", ffmpeg_logs) # This will print FFmpeg logs in your console
return result.stdout, 200, {"Content-Type": "video/mp4"}
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment