5
5

More than 5 years have passed since last update.

python時間を指定せずに録音する

Posted at

現在、PythonでGUIアプリを作成しているのですが、その中で「ボタンを押したら録音を開始し、もう一回ボタンを押したら録音を止める」というボイスレコーダーのような機能を実装したくて奮闘していたので今回はそのお話をしたいと思います。

初めはPyaudioを使っていたが

初めはpyaudioで試していたのですが、あれはあらかじめ録音の時間を指定する必要があるため、冒頭の内容を実現するのは難しいです。

G◯◯gle先生に聞いてみると

以下のソースコードを見つけました。(参考


import subprocess
import wave

print("Start recording...")
p = subprocess.Popen(("rec", "-q", "out.wav"))

input("Enter to stop: ")
p.terminate()
try:
    p.wait(timeout=1)
except subprocess.TimeoutExpired:
    p.kill()

subprocessを使えば短く簡単にwavファイルを作成できました。これならpyaudioの時よりシンプルでguiアプリに組み込みやすそうです。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5