45

I'm wondering if there's a command I can install to play an audio file from the terminal, or if I can do it with Python or a different code? I just want to type a command, have it play a sound until the sound is finished, then return to the prompt. I don't want a GUI.

Mike Pierce
  • 292
  • 3
  • 18
cheesits456
  • 1,354

6 Answers6

57

Yes you can do it with many commandline tools like mpg123, aplay , cvlc and mplayer, but I suggest the play command. To install it:

sudo apt install sox

And for playing special formats like mp3 you must install its libraries:

sudo apt install libsox-fmt-mp3

And to use it:

play music.mp3

If you want to use it with full libraries, you must install libsox-fmt-all package:

sudo apt install libsox-fmt-all
Mike Pierce
  • 292
  • 3
  • 18
5

Use Gstreamer, it should be pre-installed. It is on current Ubuntu Live Media.

gst-play-1.0 /usr/share/sounds/Yaru/stereo/system-ready.oga

I compared it to a few other answers here: https://askubuntu.com/a/1511952/40581

LiveWireBT
  • 29,597
4

Pipewire-based systems, such as Kubuntu 24.04, can use pw-play, which is already installed as part of the base OS.

John Y
  • 140
1

paplay was a better answer for me as it had the most comprehensible and stable (doesn't change on reboot) method for choosing a sound device. And it is installed by default.

To see devices for it, do

pactl list | grep Name

I'm not sure this will 100% be the case, but usually the ones you want will contain the word "output" and will not contain the word "monitor" (which is a virtual input device created from an output device).

In my case, the one I'm using is my HDMI screen, called "alsa_output.pci-0000_00_03.0.hdmi-stereo". Find the one you want, and then you'll end up with a command like:

paplay --d="alsa_output.pci-0000_00_03.0.hdmi-stereo" filename.ogg
0

You can do the same with another tool called mpg123, to install it,

sudo apt install mpg123

and then use it by,

mpg123 file.mp3
0

You can use the cvlc (Console VLC):

to play an audio file: cvlc file.wave

to play an audio stream: cvlc https://qurango.net/radio/tarateel

You can also schedule running a file or an audio stream in crontab with some tweaks:

Use pulseaudio environment settings: env | grep -E 'XDG_RUNTIME_DIR|PULSE' to add to crontab with cvlc:

0 9 * * * XDG_RUNTIME_DIR=/run/user/1000 PULSE_SERVER=/run/user/1000/pulse/native DISPLAY=:0 cvlc https://qurango.net/radio/tarateel

or you can have this in a shell file and schedule it in crontab:

Create shell script play-radio.sh with the following content:

#!/bin/bash
export DISPLAY=:0
export XDG_RUNTIME_DIR=/run/user/1000
export PULSE_SERVER=/run/user/1000/pulse/native
cvlc https://qurango.net/radio/tarateel

and make it executable: chmod +x play_radio.sh then add it to crontab: 0 9 * * * /path/to/play_radio.sh

mhmd
  • 1