2023-06-27, 22:39
I am looking for a way to retrieve the path to active captions currently being displayed, regardless of their location. Or is there a way to download the captions as a text variable?
Is it possible to fetch captions from a stream? Active captions.
I am working on creating an AI translator, but I cannot find a way to retrieve or download active captions.
Any help would be appreciated.
Unfortunately, my code is not functioning correctly.
Is it possible to fetch captions from a stream? Active captions.
I am working on creating an AI translator, but I cannot find a way to retrieve or download active captions.
Any help would be appreciated.
Unfortunately, my code is not functioning correctly.
Code:
def fetch_active_subtitle_path(kodi_ip_address: str, kodi_port: int) -> str:
# Configure logging
logging.basicConfig(filename=r"d:\_Python\KodiPlugin\log.txt", level=logging.DEBUG)
logger = logging.getLogger(__name__)
# JSON-RPC method to retrieve the current player properties
method = "Player.GetProperties"
params = {
"jsonrpc": "2.0",
"method": method,
"params": {
"playerid": 1, # Assuming the player ID is 1 for video playback
"properties": ["currentsubtitle"], # Retrieve the current subtitle property
},
"id": 1,
}
# Prepare the JSON payload
payload = json.dumps(params)
# Set the headers
headers = {
"Content-Type": "application/json",
"Connection": "Keep-Alive",
}
# Log the JSON-RPC request
logger.debug(f"JSON-RPC request: {payload}")
# Send the POST request using requests library
endpoint = f"http://{kodi_ip_address}:{kodi_port}/jsonrpc"
response = requests.post(endpoint, data=payload, headers=headers)
# Get the response content
response_content = response.text
logger.debug(f"JSON-RPC response: {response_content}")
# Check if the response is successful
if response.status_code == 200:
response_json = json.loads(response_content)
logger.debug(f"JSON-RPC response JSON: {response_json}")
if "result" in response_json and "currentsubtitle" in response_json["result"]:
current_subtitle = response_json["result"]["currentsubtitle"]
logger.debug(f"Current subtitle data: {current_subtitle}")
# Checking if the current subtitle is enabled
if current_subtitle.get("enabled", False):
active_subtitle_path = current_subtitle.get("name")
logger.debug(f"Active subtitle path: {active_subtitle_path}")
return active_subtitle_path
logger.debug("No active subtitle path found.")
return None