play music from python
from playsound import playsound
playsound('audio.mp3')
play music from python
from playsound import playsound
playsound('audio.mp3')
how to play mp3 audio in python
#TO playsound in your python code you need to install play sound module.
# To install playsound moudule-
#(1). Open Terminal
#(2). "In terminal only" type "pip install playsound"
# After installing playsound you need to write-
import playsound from playsound
# syntax of playsound command
playsound("C:\file_link\file_name")
# note: ````in python you need to specify path using double back slashes "\",
# instead of single forward or back slash````
# please read the starting comment lines they are very useful(ignore if readed)
# Enjoy :)
extract audio from .mp4 python .mp3
#!/usr/bin/env python3
"""
video_to_mp3.py
Description:
Simple script to extract MP3 audio from videos using Python.
Requirements:
- "lame"
- "ffmpeg"
If the libraries are not installed just run the following command in your terminal:
- On Mac(OS X): brew install lame ffmpeg
- On Linux(Ubuntu): sudo apt-get install lame ffmpeg
How to use the script:
Just run the following command within your terminal replacing "NAME_OF_THE_VIDEO.mp4" by the name of your video file:
$ python video_to_mp3.py NAME_OF_THE_VIDEO.mp4
Note.- The video must be within the same directory of the python script, otherwise provide the full path.
"""
import sys
import os
import time
def video_to_mp3(file_name):
""" Transforms video file into a MP3 file """
try:
file, extension = os.path.splitext(file_name)
# Convert video into .wav file
os.system('ffmpeg -i {file}{ext} {file}.wav'.format(file=file, ext=extension))
# Convert .wav into final .mp3 file
os.system('lame {file}.wav {file}.mp3'.format(file=file))
os.remove('{}.wav'.format(file)) # Deletes the .wav file
print('"{}" successfully converted into MP3!'.format(file_name))
except OSError as err:
print(err.reason)
exit(1)
def main():
# Confirm the script is called with the required params
if len(sys.argv) != 2:
print('Usage: python video_to_mp3.py FILE_NAME')
exit(1)
file_path = sys.argv[1]
try:
if not os.path.exists(file_path):
print('file "{}" not found!'.format(file_path))
exit(1)
except OSError as err:
print(err.reason)
exit(1)
video_to_mp3(file_path)
time.sleep(1)
if __name__ == '__main__':
main()
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us