LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 885|回复: 4

学习shell中,贴一个“懒人播放音乐”脚本。

[复制链接]
发表于 2005-7-23 18:00:29 | 显示全部楼层 |阅读模式
只是一个简单的练习shell 的脚本,
请多多指教。



  1. #!/bin/bash
  2. ###################################################
  3. # Inspired by the idea of Apple Ipod Shuffle
  4. # This script makes playing music easier than ever before
  5. ###################################################
  6. version=0.1
  7. author="LL"
  8. release="first release"
  9. name=`basename $0`

  10. # mpg321 — Free clone of mpg123, a command-line mp3 player
  11. engine=mpg321
  12. #play how many songs at one time
  13. num=10
  14. #this is the dynamic playlist generated each time
  15. playlist=$HOME/.ipod_playlist
  16. #setting the volume (max 100) as default
  17. vol="-g 70"

  18. #aliases to the options of the engine
  19. shuffle="-z"
  20. quiet="-q"
  21. list="--list"

  22. # This function generates a playlist
  23. Shuffle()
  24. {
  25. #search all the mp3 files in user's HOME and put them all in the playlist
  26. find $HOME -name *[Mm][pP]3 >$playlist
  27. #find how many songs found
  28. all=`wc -l  $playlist`
  29. all=${all%% *}

  30. # if no songs found, then nothing to do but exit
  31. if [ $all -eq 0 ]
  32. then
  33. exit 2
  34. fi

  35. # if the number of all the songs is not more than $num, just play them all
  36. # else generate a new playlist
  37. if [ $all -gt $num ]
  38. then
  39. playlist_new=${playlist}_new
  40. touch $playlist_new
  41. fill=0
  42. while [  $fill -lt $num ]
  43. do
  44. #generate random number to choose songs and put them in the new playlist
  45. choose=$(($RANDOM % $all + 1))
  46. #add the song into the new playlist if it's not added yet
  47. current=`head -$choose $playlist | tail -1`
  48. compare=`grep "$current" $playlist_new`
  49. if [ "$compare" = "" ]
  50. then
  51. echo $current >> $playlist_new
  52. fi
  53. #calculate how many songs have been filled in the new playlist
  54. fill=`wc -l $playlist_new`
  55. fill=${fill%% *}
  56. done
  57. #replace the old playlist with the new one
  58. mv -f $playlist_new $playlist
  59. fi
  60. }

  61. #play music
  62. Play()
  63. {
  64. #check if engine has been already playing
  65. if [ "`pgrep $engine`" != "" ]
  66. then
  67. exit 1
  68. fi
  69. Shuffle
  70. $engine $quiet $shuffle $vol $list $playlist &
  71. }

  72. #exit the music player
  73. Stop()
  74. {
  75. if [ "`pgrep $engine`" != "" ]
  76. then
  77. kill `pgrep $engine`
  78. fi
  79. }

  80. #### OK, Let's go!
  81. if [ $# -eq 0 ]
  82. then
  83. Play
  84. elif [ "$1" = "-q" ]
  85. then
  86. Stop
  87. elif [ "$1" = "-v" ]
  88. then
  89. #setting the volumes
  90. case $2 in
  91.         100|[1-9][0-9]|[1-9]) vol="-g $2";;
  92.         *) : ;;
  93. esac
  94. Play
  95. else
  96. echo "This is $name, version $version ($release), written by $author.
  97. Using the $engine as the playing engine.

  98. Here is the usage:
  99. $name                 Play $num songs in random
  100. $name -q                 Stop the playing
  101. $name -v Number        Setting volume percentage and then play music

  102. You can configure it by editting it.
  103. "
  104. fi
复制代码
发表于 2005-7-25 14:53:32 | 显示全部楼层
好淫呐~~~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-7-25 18:01:59 | 显示全部楼层
再来一个,用于 X 的。
第一个脚本尤其适合在字符界面下使用(有一点小问题,你发现了吗?)。

  1. #!/bin/bash
  2. ###################################################
  3. # Inspired by the idea of Apple Ipod Shuffle
  4. # This script makes playing music easier than ever before
  5. ###################################################
  6. #you can use another music player like: xmms, amarok...
  7. player=beep-media-player
  8. #play how many songs once
  9. num=10
  10. #the playlist generated dynamicly each time
  11. playlist=$HOME/.ipod_playlist.m3u
  12. tmplist=$HOME/.ipod_playlist.m3u.tmp
  13. #your music library path
  14. #lib=$HOME/music
  15. lib=$HOME

  16. # This function generate a random m3u playlist
  17. Genlist()
  18. {
  19. #refresh the playlist first
  20. if [ -e $playlist ]
  21. then
  22. rm -f $playlist
  23. fi
  24. touch $playlist

  25. # fill $num songs in the playlist in random
  26. fill=1
  27. while [  $fill -le $num ]
  28. do
  29. #generate random number to choose songs
  30. choose=$(($RANDOM % $all + 1))
  31. #add the song into the playlist if it's not added yet
  32. current=`head -$choose $tmplist | tail -1`
  33. compare=`grep "$current" $playlist`
  34. if [ "$compare" = "" ]
  35. then
  36. echo "#EXTM3U $fill" >>$playlist
  37. echo $current >> $playlist
  38. fill=$(($fill + 1))
  39. fi
  40. done
  41. }

  42. #search mp3  in user's music library and put them all in the tmp playlist
  43. #if you find it is unneccessary to search them everytime, just search at some time you like
  44. find "$lib" -name *[Mm][pP]3 >$tmplist
  45. #you can add other music types here like ogg, flac
  46. #find "$lib" -name *[Oo][Gg][Gg] >>$tmplist

  47. #calculate how many songs found in all
  48. all=`wc -l  $tmplist`
  49. all=${all%% *}

  50. # if no songs found, nothing to do but exit
  51. if [ $all -eq 0 ]
  52. then
  53. exit 1
  54. fi

  55. #if the number of songs are not more than $num, just play them all
  56. if [ $all -le $num ]
  57. then
  58. num=$all
  59. fi

  60. #make a playlist
  61. Genlist

  62. #call your player to play it at the background
  63. $player $playlist &
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-7-25 19:44:56 | 显示全部楼层
Yet, another one. Made a few changes to the previous one.


  1. #!/bin/bash
  2. ###################################################
  3. # Inspired by the idea of Apple Ipod Shuffle
  4. # This script makes playing music easier than ever before
  5. ###################################################
  6. #you can use another music player like: xmms, amarok...
  7. player=beep-media-player
  8. #play how many songs once
  9. num=5
  10. #the playlist generated dynamicly each time
  11. playlist=$HOME/.ipod_playlist.m3u
  12. tmplist=$HOME/.ipod_playlist.m3u.tmp
  13. oldlist=$HOME/.ipod_playlist.m3u.recent
  14. #your music library path
  15. #lib=$HOME/music
  16. lib=$HOME

  17. # This function generate a random m3u playlist
  18. Genlist()
  19. {
  20. # fill $num songs in the playlist in random
  21. fill=1
  22. while [  $fill -le $num ]
  23. do
  24. #generate random number to choose songs
  25. choose=$(($RANDOM % $all + 1))
  26. #check if the song has been added already, compare should return nothing if not added
  27. current=`head -$choose $tmplist | tail -1`
  28. compare=`grep "$current" $playlist`
  29. #check if the song has been played recently, compare should be empty if not
  30. compare2=`grep "$current" $oldlist`

  31. # put the selected songs in the playlist and mark it as being played recently
  32. if [ "$compare" = "" ] && [ "$compare2" = "" ]
  33. then
  34. echo "#EXTM3U $fill" >>$playlist
  35. echo $current >> $playlist
  36. echo $current >> $oldlist
  37. fill=$(($fill + 1))
  38. fi
  39. done
  40. }

  41. #search mp3  in user's music library and put them all in the tmp playlist
  42. #if you find it is unneccessary to search them everytime, just search at some time you like
  43. find "$lib" -name *[Mm][pP]3 >$tmplist
  44. #you can add other music types here like ogg, flac
  45. #find "$lib" -name *[Oo][Gg][Gg] >>$tmplist

  46. #calculate how many songs found in all
  47. all=`wc -l  $tmplist`
  48. all=${all%% *}
  49. # if no songs found, nothing to do but exit
  50. if [ $all -eq 0 ]
  51. then
  52. exit 1
  53. fi

  54. #check the old playlist
  55. if [ ! -e $oldlist ]
  56. then
  57. touch $oldlist
  58. fi
  59. #calculate how many songs have been plalyed recently
  60. old=`wc -l  $oldlist`
  61. old=${old%% *}

  62. #refresh the playlist first
  63. if [ -e $playlist ]
  64. then
  65. rm -f $playlist
  66. fi
  67. touch $playlist
  68. #make a  new playlist
  69. if [ $all -le $num ]
  70. # the number of songs are not more than $num, just play them all
  71. then
  72. num=$all
  73. Genlist
  74. elif [ $(($all - $old)) -lt $num ]
  75. then
  76. # a circle will be complete this time, play the left ones first
  77. total=$num
  78. num=$(($all - $old))
  79. Genlist
  80. #rewrite the recently played list
  81. echo "`tail -$num $oldlist`" >$oldlist
  82. num=$(($total - $num))
  83. Genlist
  84. else
  85. Genlist
  86. fi

  87. #call your player to play it at the background
  88. $player $playlist &
复制代码
回复 支持 反对

使用道具 举报

发表于 2005-7-26 09:47:28 | 显示全部楼层
拿来学习学习
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表