conplay 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env perl
  2. # Hacked by Thomas Orgis, use at your leisure.
  3. use strict;
  4. use locale;
  5. use File::Basename qw(basename dirname);
  6. my @mpg123_command = qw(mpg123 --continue -Cv --rva-album);
  7. my $listfile = "conplay.m3u";
  8. my $glob = '*.mp[123]';
  9. my $dir = shift;
  10. unless(defined $dir)
  11. {
  12. print STDERR "\nThis little wrapper runs $mpg123_command[0] on a given directory (hand in '.' for the current one), playing all $glob files therein in terminal control mode. The extra trick is that a playlist file ($listfile by default) is read and updated (created) with the position you left playback at (via 'q' key), to return on next invokation.\n";
  13. print STDERR "\nIf you give an existing file instead of a directory, or some non-existing path, as first and only paramter, it is used as playlist name and the directory part is used as the base directory for playback.\n";
  14. print STDERR "\nThe name stands for CONtinued PLAYback. What did you think?;-)\n\n";
  15. exit;
  16. }
  17. if(-f $dir or (not -e $dir))
  18. {
  19. $listfile = basename($dir);
  20. $dir = dirname($dir);
  21. }
  22. chdir($dir) or die "Cannot enter $dir ($!)!\n";
  23. print STDERR "Playing things in: $dir\n";
  24. my @files;
  25. my $entry = 1;
  26. my $frame = 0;
  27. if(-e $listfile)
  28. {
  29. open(LIST, '<', $listfile) or die "Cannot read playlist ($!)!\n";
  30. while(<LIST>)
  31. {
  32. chomp;
  33. unless(/^#/)
  34. {
  35. push(@files, $_);
  36. }
  37. elsif(/^#\s*current entry:\s*(\d+)$/)
  38. {
  39. $entry = $1;
  40. }
  41. elsif(/^#\s*current frame:\s*(\d+)$/)
  42. {
  43. $frame = $1;
  44. }
  45. }
  46. close(LIST);
  47. }
  48. else
  49. {
  50. @files = get_files($glob);
  51. write_list();
  52. }
  53. unless(@files)
  54. {
  55. print STDERR "There are no files to play.\n";
  56. exit;
  57. }
  58. if($entry < 0 or $entry > $#files or $frame < 0)
  59. {
  60. die "You got bad data in your playlist file (mismatch between current entry and total count, bad frame index). Clean that up.\n";
  61. }
  62. push(@mpg123_command, '-k', $frame, '--listentry', $entry, '-@', $listfile);
  63. print STDERR "running player:\n\t@mpg123_command\n\n";
  64. open(MPG123, '-|', @mpg123_command) or die "Cannot run mpg123!";
  65. while(<MPG123>)
  66. {
  67. print STDOUT $_;
  68. if(/^\[CONTINUE\]\s+track\s+(\d+)\s+frame\s+(\d+)/)
  69. {
  70. $entry = $1;
  71. $frame = $2;
  72. }
  73. if(/^\[BOOKMARK\]\s+track\s+(\d+)\s+frame\s+(\d+)/)
  74. {
  75. print STDERR "\nGot bookmark at track $1, frame $2; not yet doing anything with that, besides storing.\n";
  76. $entry = $1;
  77. $frame = $2;
  78. }
  79. }
  80. close(MPG123);
  81. print STDERR "Continue point is in track $entry, frame $frame.\n";
  82. write_list();
  83. sub write_list
  84. {
  85. unless(@files)
  86. {
  87. print STDERR "Refusing to write empty playlist.\n";
  88. return;
  89. }
  90. open(LIST, '>', $listfile) or die "Cannot write Playlist";
  91. print LIST "#M3U\n";
  92. print LIST "#current entry: $entry\n";
  93. print LIST "#current frame: $frame\n";
  94. for my $f (@files)
  95. {
  96. print LIST "$f\n";
  97. }
  98. close(LIST);
  99. }
  100. sub get_files
  101. {
  102. my $glob = shift;
  103. my @files;
  104. open(FIND, '-|', 'find', '.', '-type', 'f', '-name', $glob) or die "Cannot exec find to find files: ($!)\n";
  105. @files = <FIND>;
  106. close(FIND);
  107. chomp(@files);
  108. return sort @files;
  109. }