# Talk Link
This a tool to generate a talk link from a youtube video. It uses the AssemblyAI API to transcribe the audio and then assigns speakers to the transcript. It then generates an html page with the transcript and the embedded video. Offers features like searching, filter text based on speaker, and more.

## End to end example
First install the package:
```bash
pip install -e .
```

Then run the example script:
```bash
ASSEMBLYAI_API_KEY=<api key> python example/example.py
```

The example is based on this [video](https://www.youtube.com/watch?v=1emwCJbe9y8). To view it:
```bash
open example/html/1emwCJbe9y8_talklink_output.html
```

## Individual steps
### Download audio
```bash
python talklink/download.py --url "<youtube url>"
```

### Generate a transcript
```bash
ASSEMBLYAI_API_KEY=<api key> python talklink/transcribe.py --file_path "<audio file>" --output_type "<txt or json>" --output_path "<output file>"
```

### Assign speakers
```bash
python talklink/assign_speakers.py --transcript_path <transcript path> --speaker_map_path <speaker map path> --output_path <output path>
```

where the transcript is a `txt` or `json` file generated by the `transcribe.py` script and the speaker map is a json file with the following format:

```json
{
    "A": "Mark",
    "B": "Helly R",
    "C": "Irwin",
    "D": "Dylan"
}
```

### Generate a page
```bash
python talklink/generate_enriched_page.py --input_file_path <transcript path> --video_url <youtube url> --speakers <speakers>
```

### Generate a speaker map
```bash
python talklink/generate_speaker_map.py --output_path <output_folder> "<name1>" "<name2>" 
```

### Assign speakers to a page
```bash
python talklink/assign_speakers_html.py --talklink_page <talklink page> --speaker_map <speaker map>
```

## Cost-Saving Transcription

Talklink now supports a cost-saving feature that speeds up audio before transcription and then adjusts the timestamps back to match the original audio. This can significantly reduce transcription costs with AssemblyAI, as you're charged based on the duration of the audio.

### Using the speed-up feature

When transcribing audio, you can specify a speed factor:

```bash
ASSEMBLYAI_API_KEY=<api key> python talklink/transcribe.py --audio "<audio file>" --output "<output file>" --speed 1.5
```

The `--speed` parameter accepts values greater than 1.0, such as 1.5 (for 1.5x speed) or 2.0 (for 2x speed). The higher the speed factor, the more cost savings, but there may be a trade-off with transcription accuracy.

By default, the system uses FFmpeg for audio processing if available, with a fallback to librosa. You can explicitly choose to use librosa with the `--use-librosa` flag:

```bash
ASSEMBLYAI_API_KEY=<api key> python talklink/transcribe.py --audio "<audio file>" --output "<output file>" --speed 1.5 --use-librosa
```

### Testing different speed factors

You can use the included test script to compare different speed factors:

```bash
ASSEMBLYAI_API_KEY=<api key> ./test_speed_transcribe.py --audio "<audio file>" --output-dir "./test_results"
```

This will transcribe the audio at 1.0x, 1.5x, and 2.0x speeds using FFmpeg and save the results to the specified output directory.

To test both FFmpeg and librosa implementations:

```bash
ASSEMBLYAI_API_KEY=<api key> ./test_speed_transcribe.py --audio "<audio file>" --output-dir "./test_results" --test-librosa
```

### How it works

1. The audio is sped up using either FFmpeg (preferred) or librosa
2. The sped-up audio is sent to AssemblyAI for transcription
3. The timestamps in the transcription results are adjusted to match the original audio
4. The temporary sped-up audio file is automatically deleted

This approach can reduce costs by 30-50% or more, depending on the speed factor used.

#### Implementation details

- **FFmpeg implementation**: Uses the `atempo` filter to speed up audio. For speed factors greater than 2.0, multiple `atempo` filters are chained together.
- **Librosa implementation**: Uses the `librosa.effects.time_stretch` function to speed up audio.

The FFmpeg implementation is generally faster and produces better quality audio, but requires FFmpeg to be installed on your system.