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

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

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

#1

帖子 hellojinjie » 2008-09-13 22:55

代码: 全选

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)
|-- 手机里的
|-- 未分类
上次由 hellojinjie 在 2008-09-13 23:05,总共编辑 1 次。
Say hello to everyday!
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

#2

帖子 hellojinjie » 2008-09-13 23:01

:oops: :oops:
shell 还是写不来阿 ,只能来java来充数了....(java代码中目录的特殊字符的转义没弄好,只转义了空格,别的没转义)
Say hello to everyday!
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#3

帖子 eexpress » 2008-09-13 23:23

java的。 :lol:
● 鸣学
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

#4

帖子 eexpress » 2008-09-13 23:24

-@ 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吧。
● 鸣学
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

#5

帖子 hellojinjie » 2008-09-14 9:08

刚装了 mpg321 在没有任何配置的情况下(一切都是默认设置),和 mplayer 比较,音质似乎没有mplayer好..

还有我最关心的一点就是 mpg321 占用的 cpu 太多了, 同一首歌,mplayer 占用1-3%,而mpg321 占用15-20%--
Say hello to everyday!
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

#6

帖子 hellojinjie » 2008-09-14 10:01

代码: 全选

#!/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 少了个参数,怪不的总是出错
还是自己的这个用的爽,,,,把音乐文件夹的非法字符去掉后,一切看起来都很完美的样子
上次由 hellojinjie 在 2008-09-18 22:01,总共编辑 1 次。
Say hello to everyday!
a0147520
帖子: 147
注册时间: 2007-10-04 15:23

#7

帖子 a0147520 » 2008-09-14 11:01

代码: 全选

#!/bin/bash
pid=`ps -C mplayer|awk  '{print $1}' |tail -n1` 
[ -n $pid ] && kill $pid ||  mplayer -shuffle 你自己的音乐文件夹/*
我把它在面板上做了个按钮,这样可以在背景里放音乐,满好弯的
头像
yjcong
帖子: 2470
注册时间: 2006-02-28 3:11

#8

帖子 yjcong » 2008-09-14 23:18

mplayer -shuffle *.*

即可
上次由 yjcong 在 2008-09-14 23:50,总共编辑 3 次。
一梦三年,
松风依旧,
萝月何曾老.


灵幽听微, 谁观玉颜?
灼灼春华, 绿叶含丹.
头像
yjcong
帖子: 2470
注册时间: 2006-02-28 3:11

#9

帖子 yjcong » 2008-09-14 23:23

根据楼主的改的 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
一梦三年,
松风依旧,
萝月何曾老.


灵幽听微, 谁观玉颜?
灼灼春华, 绿叶含丹.
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

#10

帖子 hellojinjie » 2008-09-15 14:19

thanks yjcong

只是你没有处理转义字符,我的音乐文件夹本来是在win下从网上下载的,有些字符是linux不适用的,
Say hello to everyday!
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

#11

帖子 hellojinjie » 2008-09-18 22:29

:D :D 还是自己的写在六楼的用的顺手
Say hello to everyday!
头像
clilinuxlove
帖子: 15
注册时间: 2008-09-18 11:35

#12

帖子 clilinuxlove » 2008-09-19 8:36

hellojinjie 写了::oops: :oops:
shell 还是写不来阿 ,只能来java来充数了....(java代码中目录的特殊字符的转义没弄好,只转义了空格,别的没转义)
原来是JAVA的啊,我说怎么看着感觉不对劲呢
Just do it.....
头像
goodluck1982
帖子: 171
注册时间: 2007-07-05 2:48

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

#13

帖子 goodluck1982 » 2008-10-18 0:14

有这么麻烦吗?

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

OK!
回复