播放随机mp3文件

我需要一个从目录中播放随机mp3的命令。 到目前为止我已经尝试过

ls *.mp3 | shuf -n 1 | omxplayer 

每个不同的玩家只是表现得没有收到文件名并吐出帮助。 谢谢您的帮助!

首先,我不喜欢Bash。 管道到进程的路径并不是那么好,并且在没有“完全如此”的情况下会导致各种各样的怪异。 话虽这么说,你在Bash中尝试做的许多与工作或操作无关的事情可以用(不幸的)更多的代码完成,但可以按照你想要的方式工作 ,用其他语言。

所以,有点无聊并且有兴趣为此创建一些东西,我去写了一个(非常粗略的)Python脚本,它可以做你想要的东西。 它可能看起来很复杂,但效果很好,我在下面的任何地方都会发表评论或解释。

注意:我只在我的系统上使用VLC播放器和Rhythmbox进行了测试,并使用xdg-open打开GUI的给定文件的默认处理程序。 就我而言,VLC是xdg-open调用的默认值。 如果您使用的是GUI并且只想使用MP3文件的默认媒体播放器,请使用xdg-open作为“播放器”。

系统上的包装要求:

  • python (Python 2.6或更高版本,但不是 Python 3)
  • python-dev (重要的Python库)

脚本安装过程:

在这里工作不是很多。 但为了使其更简单,请按照下列步骤操作:

  1. 在主目录中创建一个bin文件夹: mkdir /home/$USER/bin
  2. 将目录更改为新的“bin”文件夹: cd /home/$USER/bin
  3. 创建一个名为randommp3的文件。 使用文本编辑器将下面“代码/脚本”部分中的代码复制并粘贴到此文件中。 保存说文件。
  4. 使文件可执行: chmod +x /home/$USER/bin/randommp3
  5. 玩得开心,但请注意以下用法花絮:
    • 您别无选择,只能指定要使用的媒体播放器。 oxmplayer将是您在执行文件时代替player的内容。
    • 如果您的音乐不在 /home/$USER/Music (其中$USER是当前登录的用户),那么您还必须使用--dir参数指定完整的目录路径(或其中一个别名,如下所述)下面的“用法”部分)。 如果文件夹路径包含任何空格,则必须将其包装在单引号中(例如,对于给定路径中的“我的音乐”目录,您可以将其作为/path/to/My Music--dir参数)。

执行示例

从GUI VLC播放器的主目录中的用户音乐文件夹中打开随机MP3文件

 randommp3 vlc-wrapper 

从名为“MusicDrive”的外部驱动器打开随机MP3文件,该驱动器安装在/media文件夹中的Music Drive中,名为oxmplayer的媒体播放器中

 randommp3 --dir '/media/Music Drive' oxmplayer 

用法

 randommp3 [-h] [--dir DIRPATH] player Open a random MP3 in the player of choice, or the default player positional arguments: player The executable name of the media player to open the MP3 with. If none specified, uses the system default player. optional arguments: -h, --help show this help message and exit --dir DIRPATH, --directory DIRPATH, --music-dir DIRPATH The path to the directory where your music is stored. If the path has spaces, wrap the entire path in single-quotes ('/home/ubuntu/My Music/' for example). If not specified, the current user's Music directory in their /home/ folder is used. 

代码:(或者如果你真的很懒,你可以保存的链接 )

 #!/usr/bin/python import getpass import subprocess as sp import os import glob import random import argparse if __name__ == "__main__": # Parse arguments to the script argparser = argparse.ArgumentParser(description="Open a random MP3 in the player of choice, or the default player", add_help=True) argparser.add_argument('--dir', '--directory', '--music-dir', dest='dirpath', type=str, default=str('/home/' + getpass.getuser() + '/Music'), required=False, help="The path to the directory where your music is stored. If the path has spaces, wrap the " "entire path in single-quotes ('/home/ubuntu/My Music/' for example). If not specified, " "the current user's Music directory in their /home/ folder is used.") argparser.add_argument('player', type=str, help="The executable name of the media player " "to open the MP3 with. If none specified, " "uses the system default player.") # Using the above 'argparser' items, get the arguments for what we're going to be using. args = argparser.parse_args() # Gp to the directory your music is in. os.chdir(args.dirpath) mp3s = glob.glob('*.mp3') # Modify the directory path to make sure we have the trailing slash dirpath = args.dirpath if dirpath[-1] not in '/\\': dirpath += '/' # Actually open the MP3 file, and /dev/null to suppress output messages from the process. DEV_NULL = open(os.devnull, 'w') execpath = [args.player, '%s%s' % (dirpath, str(random.choice(mp3s)))] sp.Popen(execpath, stdout=DEV_NULL, stderr=DEV_NULL) 

这个命令应该在bash中工作。 您可能希望从MP3文件所在的父文件夹中运行它。

 find . -type f -name '*.mp3' | shuf -n 1 | xargs -d "\n" omxplayer 

或用您喜欢的媒体播放器替换omxplayer

或者另一个有效的命令是使用xdg-open来使用像@muru这样的默认播放器评论:

 xdg-open "$(find . -type f -name '*.mp3' | shuf -n 1)" 

注意:如果从shuf删除-n 1 ,它将以shuf播放所有MP3文件。 但这需要使用实际播放器而不是xdg-open 。 它只适用于第一个命令。 刚试过它。