File size: 1,451 Bytes
e8ebb3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import re
# file_path="silence_log.txt"
# Function to parse the FFmpeg silence detection log file
def parse_silence_log(file_path):
    silences = []
    
    # Regular expressions to match silence_start and silence_end lines
    silence_start_re = re.compile(r"silence_start: (\d+(\.\d+)?)")
    silence_end_re = re.compile(r"silence_end: (\d+(\.\d+)?) \| silence_duration: (\d+(\.\d+)?)")
    
    with open(file_path, 'r') as f:
        for line in f:
            # Look for silence_start
            start_match = silence_start_re.search(line)
            if start_match:
                silence_start = float(start_match.group(1))
                
            # Look for silence_end
            end_match = silence_end_re.search(line)
            if end_match:
                silence_end = float(end_match.group(1))
                silence_duration = float(end_match.group(3))
                
                # Append the start, end, and duration of each silence period
                silences.append({
                    "start": silence_start,
                    "end": silence_end,
                    "duration": silence_duration
                })
    
    return silences

# Example usage
# log_file = "silence_log.txt"
# parsed_silences = parse_silence_log(log_file)

# # Print the results
# for silence in parsed_silences:
#     print(f"Silence from {silence['start']} to {silence['end']} (Duration: {silence['duration']} seconds)")