Answers for "Playing a sound in python"

6

Play sound in python

import winsound

winsound.PlaySound('sound.wav', winsound.SND_FILENAME)
Posted by: Guest on May-04-2021
15

play music from python

from playsound import playsound
playsound('audio.mp3')
Posted by: Guest on May-06-2020
2

python record audio from microphone

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
Posted by: Guest on May-24-2020
0

Create Sound with Python

import os
import glob
from pydub import AudioSegment

video_dir = 'D:/Movies/Urdu Songs/New folder'  # Path where the videos are located
extension_list = ('*.mp4', '*.flv')

os.chdir(video_dir)
for extension in extension_list:
    for video in glob.glob(extension):
        mp3_filename = os.path.splitext(os.path.basename(video))[0] + '.mp3'
        AudioSegment.from_file(video).export(mp3_filename, format='mp3')
Posted by: Guest on August-08-2021

Python Answers by Framework

Browse Popular Code Answers by Language