Python循环处理音频分析日志行组,提取指定行键值对
从音频分析日志中按文件分组提取键值对
我之前处理这类音频分析日志时也碰到过一模一样的问题——核心就是得找准每个文件对应的日志块边界,再在块里抓取需要的信息。先明确下你的需求和示例:
日志示例
8799 Songs Remaining Loading /Volumes/My Passport for Mac/Jonas's iTunes/#4 falling.mp3 Detected sample rate: 44100 Hz, duration: 168.097959 seconds. Using transform length of 13000 for FFT, which gives us 6501 bins at 3.392308 Hz per bin. Detecting deep bass as peaks between 0.000000 Hz and 40.707692 Hz above -22 dB chosen from frequency range below 220.500000 Hz. Estimated tempo: 132.512019. Number of beats detected: 368. Total frames: 2281, about 0.073695 seconds per frame Number of deep beats: 19 Percentage of deep beats: 0.051630 Number of frames with deep bass: 707. Number of adjacent frames with deep bass: 287. Number of deep bass frames per beat: 1.921196 Percentage of deep bass frames: 0.309952 Percentage of adjacent deep bass frames: 0.125822 --- 17.513960123062134 seconds --- 8798 Songs Remaining Loading /Volumes/My Passport for Mac/Jonas's iTunes/#OccupyHipHop Inst.mp3 Detected sample rate: 44100 Hz, duration: 223.255805 seconds. Using transform length of 13000 for FFT, which gives us 6501 bins at 3.392308 Hz per bin. Detecting deep bass as peaks between 0.000000 Hz and 40.707692 Hz above -22 dB chosen from frequency range below 220.500000 Hz. Estimated tempo: 114.843750. Number of beats detected: 403. Total frames: 3030, about 0.073682 seconds per frame Number of deep beats: 28 Percentage of deep beats: 0.069479 Number of frames with deep bass: 1302. Number of adjacent frames with deep bass: 911. Number of deep bass frames per beat: 3.230769 Percentage of deep bass frames: 0.429703 Percentage of adjacent deep bass frames: 0.300660 --- 26.676011085510254 seconds ---
预期输出
/Volumes/My Passport for Mac/Jonas's iTunes/#4 falling.mp3, 0.125822 /Volumes/My Passport for Mac/Jonas's iTunes/#OccupyHipHop Inst.mp3, 0.300660
解决方案:Python脚本实现分组提取
观察日志能发现,每个文件的分析结果是用---分隔开的,我们就用这个标记来做分组。下面是一个亲测有效的Python脚本:
def parse_audio_log(log_content): result_dict = {} current_file_path = None target_value = None # 用分隔符拆分出单个文件的日志块 log_blocks = log_content.split("---") for block in log_blocks: # 跳过空块(比如日志开头/结尾的无效内容) if not block.strip(): continue # 从块里提取文件路径 block_parts = block.split() if "Loading" in block_parts: load_idx = block_parts.index("Loading") current_file_path = block_parts[load_idx + 1] # 从块里提取目标百分比数值 if "Percentage of adjacent deep bass frames:" in block: value_segment = block.split("Percentage of adjacent deep bass frames:")[1] target_value = value_segment.split()[0] # 收集完成一组就存入字典,然后重置变量 if current_file_path and target_value: result_dict[current_file_path] = target_value current_file_path = None target_value = None # 转换成预期的输出格式 output_lines = [f"{path}, {val}" for path, val in result_dict.items()] return "\n".join(output_lines) # 测试用的日志内容(实际使用时可以替换成读取文件的内容) sample_log = """8799 Songs Remaining Loading /Volumes/My Passport for Mac/Jonas's iTunes/#4 falling.mp3 Detected sample rate: 44100 Hz, duration: 168.097959 seconds. Using transform length of 13000 for FFT, which gives us 6501 bins at 3.392308 Hz per bin. Detecting deep bass as peaks between 0.000000 Hz and 40.707692 Hz above -22 dB chosen from frequency range below 220.500000 Hz. Estimated tempo: 132.512019. Number of beats detected: 368. Total frames: 2281, about 0.073695 seconds per frame Number of deep beats: 19 Percentage of deep beats: 0.051630 Number of frames with deep bass: 707. Number of adjacent frames with deep bass: 287. Number of deep bass frames per beat: 1.921196 Percentage of deep bass frames: 0.309952 Percentage of adjacent deep bass frames: 0.125822 --- 17.513960123062134 seconds --- 8798 Songs Remaining Loading /Volumes/My Passport for Mac/Jonas's iTunes/#OccupyHipHop Inst.mp3 Detected sample rate: 44100 Hz, duration: 223.255805 seconds. Using transform length of 13000 for FFT, which gives us 6501 bins at 3.392308 Hz per bin. Detecting deep bass as peaks between 0.000000 Hz and 40.707692 Hz above -22 dB chosen from frequency range below 220.500000 Hz. Estimated tempo: 114.843750. Number of beats detected: 403. Total frames: 3030, about 0.073682 seconds per frame Number of deep beats: 28 Percentage of deep beats: 0.069479 Number of frames with deep bass: 1302. Number of adjacent frames with deep bass: 911. Number of deep bass frames per beat: 3.230769 Percentage of deep bass frames: 0.429703 Percentage of adjacent deep bass frames: 0.300660 --- 26.676011085510254 seconds ---""" # 执行解析并打印结果 print(parse_audio_log(sample_log))
脚本逻辑说明
- 拆分日志块:用
---作为分隔符,把整段日志拆分成单个文件的分析片段,彻底解决分组问题。 - 提取文件路径:在每个片段里找到
Loading关键词,取它后面紧跟的字符串作为文件路径。 - 提取目标数值:定位到
Percentage of adjacent deep bass frames:的内容,提取冒号后的数值。 - 生成结果:把每组路径和数值拼成你需要的格式输出。
运行这个脚本就能得到你想要的结果啦。
内容的提问来源于stack exchange,提问作者Zhenya Warshavsky
相关产品推荐
相关产品推荐

