From f36c80a0f9b88d63eba2be1176f14288ab4745f1 Mon Sep 17 00:00:00 2001 From: zakhy <=> Date: Tue, 22 Apr 2025 15:16:19 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/workerslocation/commands/audio_sender.py | 79 ------------------- 1 file changed, 79 deletions(-) delete mode 100644 apps/workerslocation/commands/audio_sender.py diff --git a/apps/workerslocation/commands/audio_sender.py b/apps/workerslocation/commands/audio_sender.py deleted file mode 100644 index 7ff4e5a..0000000 --- a/apps/workerslocation/commands/audio_sender.py +++ /dev/null @@ -1,79 +0,0 @@ -import socket -import struct -import subprocess -import os -import sys -import datetime -import time - -RTP_PAYLOAD_TYPE = 8 -PACKET_DURATION_MS = 20 - - -def log(msg): - print( - f"[{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}", flush=True - ) - - -def convert_audio_to_g711a(input_file): - output_file = "converted_audio.g711a" - command = [ - "ffmpeg", - "-y", - "-i", - input_file, - "-ar", - "8000", - "-ac", - "1", - "-f", - "alaw", - output_file, - ] - subprocess.run(command, check=True) - with open(output_file, "rb") as f: - audio_data = f.read() - os.remove(output_file) - return audio_data - - -def start_audio_stream(audio_path, dest_ip, dest_port, ssrc, channel): - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sequence_number = 0 - timestamp = 0 - packet_size = 160 - timestamp_increment = packet_size - audio_data = convert_audio_to_g711a(audio_path) - - duration_sec = len(audio_data) / 8000 - log( - f"[{channel}] Подготовлено {len(audio_data)} байт аудио (~{duration_sec:.2f} сек) для отправки." - ) - log(f"[{channel}] Начинаем отправку аудио по RTP...") - - for i in range(0, len(audio_data), packet_size): - frame_data = audio_data[i : i + packet_size] - if not frame_data: - break - - rtp_header = struct.pack( - "!BBHII", 0x80, RTP_PAYLOAD_TYPE, sequence_number, timestamp, ssrc - ) - rtp_packet = rtp_header + frame_data - sock.sendto(rtp_packet, (dest_ip, dest_port)) - sequence_number = (sequence_number + 1) % 65536 - timestamp = (timestamp + timestamp_increment) % (2**32) - time.sleep(PACKET_DURATION_MS / 1000) - - sock.close() - log(f"[{channel}] Передача аудиофайла завершена.") - - -if __name__ == "__main__": - audio_path = sys.argv[1] - dest_ip = sys.argv[2] - dest_port = int(sys.argv[3]) - ssrc = int(sys.argv[4]) - channel = sys.argv[5] - start_audio_stream(audio_path, dest_ip, dest_port, ssrc, channel)