user1236910
user1236910

Reputation: 203

Play specified subsection of mp3 in Python 3, Windows 7

I have an MP3 file of length, say, five seconds. I want to play a specified subsection, say from second 1.4 to second 3.2. I'm working in Python 3, not Python 2, and on Windows, not Linux.

I realize that there isn't a module for Python 3 that solves my problem, but I thought perhaps I could get an MP3 player like mpg123 running as a backend and let my Python program control it. The remote commands for mpg123 are too limited though. Any other ideas? Is there even a windows program that I could use at the command line, like this:

program -start 1.4 -end 2.3 file.mp3

and call from a subprocess?

Upvotes: 0

Views: 810

Answers (1)

Sarah El-Saig
Sarah El-Saig

Reputation: 51

If you have an mplayer binary installed or you can package mplayer along, then you can use mplayer.py. It works on both Python 2 and 3 on Windows and Linux too. Once you initialized a Player you can seek with the time_pos property (float) and there's a pause and a stop method you need. Something like this:

player = mplayer.Player()
player.loadfile("musicfile.mp3")
player.time_pos = start
while player.time_pos < stop : pass
player.stop()

The downside is that there is no UI and hotkey support because mplayer is running backgrounded and the communication is through a socket. If you don't need anything else you might be better off just using mplayer itself with the -ss start time and -endpos length in seconds parameters, like this:

mplayer "music.mp3" -ss 20 -endpos 2

This will go from 00:20 to 00:22 . You can't specify milliseconds, but if you really just want to play one or more music file from point A to point B, then this (with batch or posh) is a better solution just because the one less dependency.

Upvotes: 1

Related Questions