Get the duration of an audio file using ffmpeg.
(audio_file: str)
| 53 | rprint(f"[green]🎬➡️🎵 Converted <{video_file}> to <{_RAW_AUDIO_FILE}> with FFmpeg\n[/green]") |
| 54 | |
| 55 | def get_audio_duration(audio_file: str) -> float: |
| 56 | """Get the duration of an audio file using ffmpeg.""" |
| 57 | cmd = ['ffmpeg', '-i', audio_file] |
| 58 | process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 59 | _, stderr = process.communicate() |
| 60 | output = stderr.decode('utf-8', errors='ignore') |
| 61 | |
| 62 | try: |
| 63 | duration_str = [line for line in output.split('\n') if 'Duration' in line][0] |
| 64 | duration_parts = duration_str.split('Duration: ')[1].split(',')[0].split(':') |
| 65 | duration = float(duration_parts[0])*3600 + float(duration_parts[1])*60 + float(duration_parts[2]) |
| 66 | except Exception as e: |
| 67 | print(f"[red]❌ Error: Failed to get audio duration: {e}[/red]") |
| 68 | duration = 0 |
| 69 | return duration |
| 70 | |
| 71 | def split_audio(audio_file: str, target_len: float = 30*60, win: float = 60) -> List[Tuple[float, float]]: |
| 72 | ## 在 [target_len-win, target_len+win] 区间内用 pydub 检测静默,切分音频 |
no outgoing calls
no test coverage detected