[Python] Creating a transcript with user timestamps for a Discord bot

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am using py-cord and the bot joins a meeting, starts recording, and once the end command is given it puts everything in this Discord sink.

    At the moment, my code does transcription only relative to when a user starts speaking. Example: If a user starts speaking from the start of the meeting and ends 5 mins post meeting start, and at that point a 2nd user starts speaking - in the transcription the two will be as if they spoke at the same time. The transcript is not relative to the meeting start. Edit: which is what I am looking for. The transcript should follow the proper timeline from meeting start and when people started speaking relative to the meeting start. Example. Meeting starts at 0, Speaker 1 starts speaking until min 5. At min 5:01 Speaker 2 starts speaking, Meeting ends at min 10. The transcript should be something like

    [00:00] Speaker 1:...
    [05:01] Speaker 2:...


    If they happen to speak at the same time at some point then both would appear in the transcript at that hr/min/second.

    Appreciate any help.

    async def once_done(sink: discord.sinks, channel: discord.TextChannel, *args):
    meeting_start = connections.get(channel.guild.id, {}).get("meeting_start", 0)
    user_start_times = connections.get(channel.guild.id, {}).get("user_start", {})

    await channel.send(" Transcribing...")
    await sink.vc.disconnect()

    words_list = []

    for user_id, audio in sink.audio_data.items():
    payload = {"buffer": audio.file.read()}
    response = deepgram.listen.rest.v("1").transcribe_file(payload, options)

    words = response["results"]["channels"][0]["alternatives"][0]["words"]
    words = [word.to_dict() for word in words]

    if words:
    first_word_time = words[0]["start"]
    if user_id not in user_start_times:
    user_start_times[user_id] = first_word_time

    user_offset = user_start_times[user_id] - meeting_start

    for word in words:
    words_list.append({
    "word": word["word"],
    "start": word["start"] + user_offset,
    "end": word["end"] + user_offset,
    "punctuated_word": word["punctuated_word"],
    "speaker": user_id,
    })

    words_list.sort(key=lambda x: x["start"])

    transcript = []
    current_speaker = None
    current_segment = {"speaker": None, "start": None, "end": None, "text": ""}

    for word in words_list:
    speaker = word["speaker"]
    if (speaker != current_speaker or
    (current_segment["end"] is not None and word["start"] - current_segment["end"] > 2)):
    if current_speaker is not None:
    transcript.append(current_segment)
    current_speaker = speaker
    current_segment = {
    "speaker": speaker,
    "start": word["start"],
    "end": word["end"],
    "text": word["punctuated_word"]
    }
    else:
    current_segment["text"] += " " + word["punctuated_word"]
    current_segment["end"] = word["end"]

    if current_segment["speaker"] is not None:
    transcript.append(current_segment)

    with open("transcript.txt", "w") as f:
    for segment in transcript:
    start_minutes = int(segment["start"] // 60)
    start_seconds = int(segment["start"] % 60)
    end_minutes = int(segment["end"] // 60)
    end_seconds = int(segment["end"] % 60)
    f.write(f"({start_minutes}:{start_seconds:02d} - {end_minutes}:{end_seconds:02d}) {segment['speaker']}:\n{segment['text']}\n\n")

    await channel.send(file=discord.File("transcript.txt"))
    await channel.send(" Transcription finished.")

    Continue reading...

Compartilhe esta Página