The application ffmpeg in the computer system environment cannot be used in the conda environment; ffmpeg calls the local windows microphone to read

1. ffmpegzai cannot be executed in the conda environment, but it can run in the system

import ffmpeg

stream = ffmpeg.input(r'D:\sound\222.mp4')
stream = ffmpeg.filter(stream, 'fps', fps=25, round='up')
stream = ffmpeg.output(stream, r'D:\sound\dummy2.mp4')
ffmpeg.run(stream)

An error will be reported:
File “C:\Users\loong.conda\envs\nlp\lib\subprocess.py”, line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified.
insert image description here
insert image description here

Solution, execute with all absolute paths can run normally

C:/Users/loong/.conda/envs/nlp/python.exe  D:\sound\ffmpeg_sounddevice.py

insert image description here

2. ffmpeg calls the local windows microphone to read


(This is used for other subsequent data docking, and "-f", "s16le", "-acodec", "pcm_s16le",
"-ar", "16000", sampling values ​​are formulated )

View the device name name: ffmpeg -list_devices true -f dshow -i dummy
insert image description here

ffmpeg_cmd = [
    "ffmpeg",
    "-f", "dshow",  # 使用alsa作为音频输入设备
    "-i", "audio=麦克风阵列 (适用于数字麦克风的英特尔® 智音技术)",  # 使用默认的音频输入设备(麦克风)
    "-f", "s16le",
    "-acodec", "pcm_s16le",
    "-ar", "16000",
    "-ac", "1",
    "-"
]
ffmpeg_cmd = [
    "ffmpeg",
    "-f", "dshow",  # 使用dshow作为音频输入设备(名字必须是这个,其他会报错)
    "-i", "audio=Microphone",  # 麦克风的设备名称
    "-f", "s16le",
    "-acodec", "pcm_s16le",
    "-ar", "16000",
    "-ac", "1",
    "-"
]

This command will use dshow as the audio input device and specify the microphone device named Microphone. If you're using a different microphone device name, you can replace it with your actual device name.

The other parameters have the same meaning as the previously mentioned Linux example: -f s16le specifies that the output audio format is 16-bit uncompressed PCM audio, -acodec pcm_s16le specifies that the audio codec is 16-bit uncompressed PCM audio, -ar 16000 specifies the sampling rate For 16000Hz, -ac 1 specifies the number of channels to be 1 (mono).

Finally, the output filename is replaced with - to indicate that the audio data will be streamed through standard output rather than written to a file.

Please note that the above command parameters are applicable to the Windows system, and DirectShow is used as the audio input device. If you're running on another OS, use the appropriate audio input device and command arguments.

import subprocess

import numpy as np


ffmpeg_cmd = [
    "ffmpeg",
    "-f", "dshow",  # 使用alsa作为音频输入设备
    "-i", "audio=麦克风阵列 (适用于数字麦克风的英特尔® 智音技术)",  # 使用默认的音频输入设备(麦克风)
    "-f", "s16le",
    "-acodec", "pcm_s16le",
    "-ar", "16000",
    "-ac", "1",
    "-"
]

# 创建FFmpeg进程
process = subprocess.Popen(
    ffmpeg_cmd,
    stdout=subprocess.PIPE,
    stderr=subprocess.DEVNULL,
    bufsize=1600
)


# 读取和处理音频数据
while True:
    # 从FFmpeg进程中读取音频数据
    data = process.stdout.read(frames_per_read * channels * 2)  # 每个样本16位,乘以2
    if not data:
        break
    
    # 将音频数据转换为numpy数组
    samples = np.frombuffer(data, dtype=np.int16)
    samples = samples.astype(np.float32)
    # samples = MinMaxScaler(feature_range=(-1, 1)).fit_transform(samples.reshape(-1, 1))
    samples /= 32768.0  # 归一化到[-1, 1]范围
    ##数据后续处理逻辑

ffmpeg saves a local computer every few seconds

Local sampling values ​​"-f", "wav",
"-acodec", "pcm_s16le",
"-ar", "44100",
"-ac", "2", the local computer can play normally

import subprocess
import time
import numpy as np



i = 0
while True:
    start_time = time.time()

    output_file = f"D:\llm\output{i}.wav"  # 创建输出文件名,例如output0.wav, output1.wav, ...

    ffmpeg_cmd = [
        "ffmpeg",
        "-f", "dshow",
        "-i", "audio=麦克风阵列 (适用于数字麦克风的英特尔® 智音技术)",
        "-f", "wav",
        "-acodec", "pcm_s16le",
        "-ar", "44100",
        "-ac", "2",
        output_file,
    ]
    process = subprocess.Popen(ffmpeg_cmd)


    # 等待两秒
    time.sleep(2)

    # 终止ffmpeg进程
    process.terminate()

    i += 1

Guess you like

Origin blog.csdn.net/weixin_42357472/article/details/131441793