分页: 1 / 1

我的第一个带点实际用处的 shell script,随机播放子目录中的音乐

发表于 : 2008-09-13 22:55
hellojinjie

代码: 全选

mplayer `java -cp /home/jj/scripts/binary RandomMusicDirectory`
:lol: :lol: :lol: :lol:

代码: 全选

import java.io.File;
import java.util.Random;
import java.util.regex.*;

public class RandomMusicDirectory {

	public static void main(String[] args) {

		Random rand = new Random(System.currentTimeMillis());
		File dir = new File("/media/JOY/music");
		File[] musicDir = dir.listFiles();

		boolean flag = true;
		String dirString = null;	
		while (flag) {
			int i = rand.nextInt(100000) % musicDir.length;
			if (musicDir[i].isDirectory()) { 
				dirString = musicDir[i].getAbsolutePath() + "/*";
				flag = false;
			}
		}
		Matcher m = Pattern.compile("([\\s])").matcher(dirString); // 处理空格
		if (m.find())
			dirString = m.replaceAll("\\\\" + m.group(1));
		System.out.print(dirString);
	}
}

代码: 全选

jj@hellojinjie:/media/JOY$ tree -d music
music
|-- Hilary.Duff.-.Artist.Karaoke.Series
|-- KristofferRagnstamSweetBills(2006)
|   `-- Cover
|-- Leona Lewis - Spirit
|-- Leona.Naess.-.[I.Tried.To.Rock.You.But.You.Only.Roll].专辑.(MP3)
|-- MusicOfTheSun
|-- The Academy Is... - Fast Times At Barrington High
|-- bandari
|-- 戴佩妮.-.[Penny].专辑.(MP3)
|-- 戴佩妮.-.[怎样].专辑.(mp3)
|-- 手机里的
|-- 未分类

发表于 : 2008-09-13 23:01
hellojinjie
:oops: :oops:
shell 还是写不来阿 ,只能来java来充数了....(java代码中目录的特殊字符的转义没弄好,只转义了空格,别的没转义)

发表于 : 2008-09-13 23:23
eexpress
java的。 :lol:

发表于 : 2008-09-13 23:24
eexpress
-@ list, --list list
Use the file list for a playlist. The list should be in a format of filenames
followed by a line feed. Multiple -@ or --list specifiers will be ignored; only
the last -@ or --list option will be used. The playlist is concatenated with
filenames specified on the command-line to produce one master playlist. A file-
name of '-' will cause standard input to be read as a playlist.

-z, --shuffle
Shuffle playlists and files specified on the command-line. Produces a randomly-
sorted playlist which is then played through once.

man mpg321吧。

发表于 : 2008-09-14 9:08
hellojinjie
刚装了 mpg321 在没有任何配置的情况下(一切都是默认设置),和 mplayer 比较,音质似乎没有mplayer好..

还有我最关心的一点就是 mpg321 占用的 cpu 太多了, 同一首歌,mplayer 占用1-3%,而mpg321 占用15-20%--

发表于 : 2008-09-14 10:01
hellojinjie

代码: 全选

#!/bin/bash
currentTimeMillis=`date +%N`
i=`tree -dif -L 1 /media/JOY/music | wc -l`
dirCount=`expr $i - 3` #这里为什么要减3啊?
m=`expr $currentTimeMillis % $i + 2`
mplayer `tree  /media/JOY/music -dif -L 1 |head -n  $m |tail -n 1`/*
tree 少了个参数,怪不的总是出错
还是自己的这个用的爽,,,,把音乐文件夹的非法字符去掉后,一切看起来都很完美的样子

发表于 : 2008-09-14 11:01
a0147520

代码: 全选

#!/bin/bash
pid=`ps -C mplayer|awk  '{print $1}' |tail -n1` 
[ -n $pid ] && kill $pid ||  mplayer -shuffle 你自己的音乐文件夹/*
我把它在面板上做了个按钮,这样可以在背景里放音乐,满好弯的

发表于 : 2008-09-14 23:18
yjcong
mplayer -shuffle *.*

即可

发表于 : 2008-09-14 23:23
yjcong
根据楼主的改的 Dir为musikd文件夹

代码: 全选

#!/bin/bash

Dir=""

pl()
{
currenttime=`date +%N`
num=`expr $currenttime % $i + 2` 
loo=1
for fil in *.*;do
if (( $loo == $num ));then
mplayer $fil
fi
(( loo++ ))
done
pl
}

cd $Dir
i=`ls|wc -l`
pl

发表于 : 2008-09-15 14:19
hellojinjie
thanks yjcong

只是你没有处理转义字符,我的音乐文件夹本来是在win下从网上下载的,有些字符是linux不适用的,

发表于 : 2008-09-18 22:29
hellojinjie
:D :D 还是自己的写在六楼的用的顺手

发表于 : 2008-09-19 8:36
clilinuxlove
hellojinjie 写了::oops: :oops:
shell 还是写不来阿 ,只能来java来充数了....(java代码中目录的特殊字符的转义没弄好,只转义了空格,别的没转义)
原来是JAVA的啊,我说怎么看着感觉不对劲呢

Re: 我的第一个带点实际用处的 shell script,随机播放子目录中的音乐

发表于 : 2008-10-18 0:14
goodluck1982
有这么麻烦吗?

先要cd到你想要去的目录
find . -iname '*.mp3' -o -iname '*.ogg' > list
#可以继续添加其它音频格式
然后
mplayer -shuffle -playlist list

OK!