How to use the command `mpg321` (with examples)

How to use the command `mpg321` (with examples)

mpg321 is a high-performance MPEG 1.0/2.0/2.5 audio player for Layer 1, 2, and 3. It is a drop-in replacement for the previously non-free mpg123 player. It allows you to play audio files, directories, and URLs, with various options such as playing files repeatedly, playing files randomly, enabling basic keys while playing, and more.

Use case 1: Play an audio source exactly N times (N=0 means forever)

Code:

mpg321 -l N path/to/file_a|URL path/to/file_b|URL ...

Motivation: This use case is useful when you want to play a particular audio source a specific number of times. For example, if you want to play a background music file during a presentation, you can set N to the number of slides in your presentation, so the audio will play automatically in sync with the presentation.

Explanation:

  • -l N: Specifies the number of times the audio source should be played. N=0 means the source will be played forever.
  • path/to/file_a|URL: The path or URL of the audio file to be played.

Example output:

Suppose you have a file called “music.mp3” in the “Downloads” directory, and you want to play it 3 times. The command would be:

mpg321 -l 3 Downloads/music.mp3

Output: Plays the “music.mp3” file 3 times.

Use case 2: Play a directory recursively

Code:

mpg321 -B path/to/directory

Motivation: This use case is useful when you have multiple audio files in a directory and you want to play them all, including files in subdirectories. It saves you from manually specifying the path to each file.

Explanation:

  • -B: Enables directory recursion, which means files in subdirectories will also be played.
  • path/to/directory: The path to the directory containing the audio files.

Example output:

Suppose you have a directory called “music” containing various audio files, including files in subdirectories. The command would be:

mpg321 -B music/

Output: Plays all the audio files in the “music” directory and its subdirectories.

Use case 3: Enable Basic Keys while playing

Code:

mpg321 -K path/to/file_a|URL path/to/file_b|URL ...

Motivation: This use case is useful when you want to control the audio playback while it is playing. Enabling basic keys allows you to adjust the volume, skip songs, and mute/unmute the audio.

Explanation:

  • -K: Enables Basic Keys functionality.
  • path/to/file_a|URL: The path or URL of the audio file to be played.

Example output:

Suppose you have a file called “song.mp3” in the current directory, and you want to control the playback using basic keys. The command would be:

mpg321 -K song.mp3

Output: Plays the “song.mp3” file and allows you to use the Basic Keys functionality to control the playback.

Use case 4: Play files randomly until interrupted

Code:

mpg321 -Z path/to/file_a|URL path/to/file_b|URL ...

Motivation: This use case is useful when you want to play your audio files randomly without any specific order. It adds an element of surprise and variety to your listening experience.

Explanation:

  • -Z: Plays the files randomly until interrupted.
  • path/to/file_a|URL: The path or URL of the audio file to be played.

Example output:

Suppose you have three files named “song1.mp3”, “song2.mp3”, and “song3.mp3” in the current directory, and you want to play them randomly. The command would be:

mpg321 -Z song1.mp3 song2.mp3 song3.mp3

Output: Plays the files “song1.mp3”, “song2.mp3”, and “song3.mp3” randomly until interrupted.

Use case 5: Shuffle the files before playing them once

Code:

mpg321 -z path/to/file_a|URL path/to/file_b|URL ...

Motivation: This use case is similar to the previous one in terms of playing files randomly. However, in this case, the files are shuffled first and then played once in the shuffled order.

Explanation:

  • -z: Shuffles the files before playing them once.
  • path/to/file_a|URL: The path or URL of the audio file to be played.

Example output:

Suppose you have three files named “song1.mp3”, “song2.mp3”, and “song3.mp3” in the current directory, and you want to shuffle and play them once. The command would be:

mpg321 -z song1.mp3 song2.mp3 song3.mp3

Output: Shuffles the files “song1.mp3”, “song2.mp3”, and “song3.mp3” and plays them once in the shuffled order.

Use case 6: Play all files in the current directory and subdirectories, randomly (until interrupted), with Basic Keys enabled

Code:

mpg321 -B -Z -K .

Motivation: This use case is useful when you have multiple audio files in the current directory and its subdirectories, and you want to play them randomly with the ability to control the playback using basic keys.

Explanation:

  • -B: Enables directory recursion, which means files in subdirectories will also be played.
  • -Z: Plays the files randomly until interrupted.
  • -K: Enables Basic Keys functionality.
  • .: Refers to the current directory.

Example output:

Suppose you have multiple audio files in the current directory and its subdirectories, and you want to play them randomly with the ability to control the playback using basic keys. The command would be:

mpg321 -B -Z -K .

Output: Plays all the audio files in the current directory and its subdirectories randomly until interrupted, with the Basic Keys functionality enabled.

Conclusion:

The mpg321 command provides a comprehensive set of options to play audio files, directories, and URLs. With its ability to play files repeatedly, play files randomly, enable basic keys, and more, it offers a flexible solution for managing and enjoying your audio collection. Use the provided examples to take advantage of the different features based on your needs and preferences.

Related Posts

How to use the command "warp-cli" (with examples)

How to use the command "warp-cli" (with examples)

“warp-cli” is the official command-line client for Cloudflare’s WARP service. It allows users to register devices, connect to the WARP service, disconnect from it, check the connection status, and access help documentation.

Read More
Docker Command Examples (with examples)

Docker Command Examples (with examples)

List all docker containers (running and stopped) Code: docker ps --all Motivation: The docker ps command is used to list all the currently running containers.

Read More