How to Use the Command 'mpg123' (with Examples)
- Linux
- December 17, 2024
The mpg123
command is a console-based MPEG audio player used to play MP3 audio files directly from the command line interface. Its simplicity and efficiency make it a popular choice for users who prefer utilizing terminal applications or those who are managing audio playback on headless systems (systems without a graphical user interface). mpg123
provides a variety of controls for navigating through audio tracks and streaming audio data from files or standard input.
Use Case 1: Play the Specified MP3 Files
Code:
mpg123 path/to/file1.mp3 path/to/file2.mp3 ...
Motivation:
This use case is particularly beneficial when you have a list of MP3 files that you want to play sequentially. It enables efficient and straightforward playback of multiple audio tracks with a single command.
Explanation:
mpg123
: The command-line tool used to initiate audio playback.path/to/file1.mp3 path/to/file2.mp3 ...
: A list of one or more paths leading to the MP3 files you intend to play. By specifying each file,mpg123
understands the sequence in which the files should be played.
Example Output:
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
Version 1.26.4; Written and copyright by Michael Hipp and others
free software (LGPL) without any warranty, basing on mpg123 0.59r
Playing MPEG stream 1 of 2: file1.mp3 ...
Title: Example Song 1 Artist: Sample Artist
...
Playing MPEG stream 2 of 2: file2.mp3 ...
Title: Example Song 2 Artist: Sample Artist
...
Use Case 2: Play the MP3 from stdin
Code:
cat file.mp3 | mpg123 -
Motivation:
Using this method is advantageous when you want to stream an MP3 file’s contents directly to mpg123
without needing to specify a file path explicitly. This can be useful in scripting or automation where direct file manipulation is preferred.
Explanation:
cat file.mp3
: This command reads the contents of thefile.mp3
.|
: A pipe that channels the output of the left command as input to the right command.mpg123 -
: The-
indicates that the input will be read from the standard input rather than a file.
Example Output:
Playing MPEG stream from stdin ...
Title: Example Song Stream Artist: Streaming Artist
Use Case 3: Jump Forward to the Next Song
Code:
f
Motivation:
This command is critical when you are manually intervening in the playlist to skip to the next track. Perhaps you have encountered a song that you are not in the mood to listen to, or you want to ensure a specific track starts right away.
Explanation:
f
: A keyboard command used during playback to skip to the next track in the playlist.
Example Output:
Jumping to next track...
Playing MPEG stream 2 of 2: file2.mp3 ...
Title: Example Song 2 Artist: Sample Artist
Use Case 4: Jump Back to the Beginning of the Song
Code:
b
Motivation:
If you want to replay the current song from the start, perhaps to appreciate a particular segment again, this command offers a quick and effective solution to reset to the beginning.
Explanation:
b
: A keyboard command allowing the replay of the current track from the beginning.
Example Output:
Restarted playing: Example Song 1 by Sample Artist
Use Case 5: Stop or Replay the Current File
Code:
s
Motivation:
This plays a useful role when you need a pause in audio playback for any reason. It could be useful during a lecture or meeting, where you pause the playback until you are ready to resume.
Explanation:
s
: A keyboard shortcut to toggle between stopping and replaying the current audio playback.
Example Output:
Stopping/restarting current track: Example Song 1
Use Case 6: Fast Forward
Code:
.
Motivation:
Efficient for users who need to skip ahead within a song or speech. Fast-forwarding can be particularly useful for lengthy files where only a specific part is of interest.
Explanation:
.
: A key press that signals mpg123 to advance quicker through the audio file by an unspecified increment.
Example Output:
Fast-forwarding within current track...
Use Case 7: Quit
Code:
q
Motivation:
This is the ultimate command for closing the mpg123 program when you’re done listening. It is designed to swiftly terminate playback with minimal effort.
Explanation:
q
: A direct quit command for terminating the audio player gracefully.
Example Output:
Goodbye from mpg123!
Conclusion:
mpg123
is a versatile command-line audio playback tool, offering a range of functionalities for managing and interacting with MP3 files through simple commands. Whether you need to manipulate audio tracks directly from the terminal or incorporate it into a script for audio automation, mpg123
provides efficient solutions for varied audio playback tasks.