Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Sound-player standalone
#1
Gone look at making at making simple standalone Sound-player that can play most format eg .mp3.
Gone make it for Windows and Linux.
The goal is one standalone file(Windows:play.exe and Linux:play) this file can be moved to any OS and just work.

Install
Tool for this pipenv, pyinstaller, pyglet and Click.
Start with pipenv which is great for keeping track of dependencies,it's a marriage Heart of pip and virtualenv.
# Install pipenv
pip install -U pipenv

# Now pipenv in a folder eg (mkdir)sound_env
pipenv install pyinstaller, pyglet, click

# Activate sound_env folder
pipenv shell
Take a look at dependencies in Windows and Linux.
pipenv graph
See all dependencies is installed also newer pypiwin32 and not older win32api,
which could be used i doing this without using virtual environment.

Code
import pyglet
import click
from msvcrt import getch
pyglet.lib.load_library('avbin.dll')
pyglet.have_avbin=True

@click.command()
@click.argument('file', type=click.Path(exists=True), metavar='<file>')
def play(file):
    '''
    Play sound file from command line Supported format:
    AU,MP2,MP3,OGG/Vorbis,WAV,WMA
    
    \b
    Example:
    play song.mp3
    # File with space use quote.
    play "Clean Bandit.mp3"        
    <Space> quit the song
    '''
    music = pyglet.media.load(file)
    music.play()
    while True:
        key = ord(getch())
        # 32 use space to quit
        if key == 32:
            break
        pyglet.app.run()

if __name__ == '__main__':
    play()
Test code:
E:\sound_env
λ which python
/c/Users/Tom/.virtualenvs/sound_env-zFUYyERb/Scripts/python

E:\sound_env
λ python play.py "Clean Bandit.mp3"

E:\sound_env
λ
Linux:
Pyglet for playing which is using AVbin avbin.dll(windows) and libavbin.so.10(Linux),
even if i try to make clear so do not Pyinstaller find these files,so add it manually.

Click is great command line tool,type=click.Path(exists=True) will do error checking so we don't.
metavar='<file>' will use docstring of function when do play --help

Build
Build with Pyinstaller:
Command for Windows:
pyinstaller --onefile --console --icon=sound.ico --add-data avbin.dll;. play.py

Command for Linux:
pyinstaller --onefile --console --icon=sound.ico --add-data libavbin.so.10:. play.py
There is a little difference ;. Win and Linux :..
. - Destination path of dll in .exe or play file(Linux).
Every time Pyinstaller run a .spec file is generated,in this case play.spec
This file can be edit to add more stuff,then run like this.
pyinstaller --clean play.spec

Usage
It's a very simple player,eg place play.exe it environment variable Path(Windows),
and it work from anywhere in cmd.
Example:
# Excutebale
mint@mint ~/sound_env/dist $ chmod +x play

# Use
mint@mint ~/sound_env/dist $ ./play --help
Usage: play [OPTIONS] <file>

  Play sound file from command line Supported format:
  AU,MP2,MP3,OGG/Vorbis,WAV,WMA

  Example:
  play song.mp3
  # File with space use quote.
  play "Clean Bandit.mp3"        
  <Ctrl+c> quit the song

# Play Song Linux  
mint@mint ~/sound_env/dist $ ./play faded.mp3

# Play Song windows
C:\1>play "Clean Bandit.mp3"
Look with icon Windows:
İmage


Take away
A look a pipenv a cool that keep track of dependencies and make pip and virtual environment work with one command.
Pyinstaller a good tool for bundle all dependencies is single single package.
Click for creating beautiful command line interfaces,in my option the best of the bunch.
Quote
#2
Over has made a standalone Sound-player where Python is not required.
Now gone take a look making the same Sound player for Python with wheel.
Wheel can also be distribute bye PyPi(soon Warehouse),example could have been pip install play if i had uploaded my wheel.

For making wheel will be using setup.py.
So bye doing pip install wheel,should all be included for 3-party user.
data_files: this add dll and so file to OS.
install_requires: This will install packages.
entry_points: This is cool function that will actually make .exe for Windows as executable for Linux.
# setup.py
from setuptools import setup
 
__author__ = 'snippsat'
setup(
   name="play",
   version='0.1',
   py_modules=['play'],
   description="Play sound files",
   url='https://python-forum.io',
   author_email='something@python.io',
   python_requires='>=3.4',
   # Add required files to Windows and Linux
   data_files=[       
       ('Scripts', ['avbin.dll']),
       ('usr/lib', ['libavbin.so.10'])
       ],
   classifiers=[
       'Programming Language :: Python :: 3.4',
       'Programming Language :: Python :: 3.5',
       'Programming Language :: Python :: 3.6',
    ],
   # These will be installed when "pip install wheel" run
   install_requires=[       
       'pyglet',
       'click',
   ],
   # Make executable eg for Windows will make .exe in Scripts folder
   entry_points='''\
        [console_scripts]
        play=play:play''',
)
Now i activate folder again pipenv shell.
Build the wheel:
E:\sound_env
λ python setup.py bdist_wheel
Now navigate to dist folder and install wheel.
E:\sound_env
λ cd dist

E:\sound_env\dist
λ ls
play-0.1-py3-none-any.whl

E:\sound_env\dist
λ pip install play-0.1-py3-none-any.whl
So now in Python Script folder in virtual environment i have.
play.exe
avbin.dll
Then this should work from anywhere in cmd,as long as Scripts folder is in environment variable Path for Windows.
Larz60+ likes this post
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)