LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Looking for code to play random video at random playback point. (https://www.linuxquestions.org/questions/linux-general-1/looking-for-code-to-play-random-video-at-random-playback-point-4175678439/)

linustalman 07-09-2020 01:59 PM

Looking for code to play random video at random playback point.
 
Hi.

I'm looking for a command to play a random video from a particular folder and play the file at a random point, e.g. 63m16s in to a film, etc.

Thanks.

teckk 07-09-2020 03:11 PM

I would think that about any media player where you can launch it with a index time will do that.

Example random number from 30 - 90
Code:

ran=$((30 + RANDOM % 60))

mplayer -ss "$ran" file.mp4

Choose a sudorandom file
Code:

f=(file1.mp4 file2.mp4 file3.mp4 file4.mp4 file5.mp4
        file6.mp4 file7.mp4 file8.mp4 file9.mp4 file10.mp4)

for i in {1..20}; do
    select_f=${f[$RANDOM % ${#f[@]} ]}
    echo "$select_f"
    sleep .5
done


linustalman 07-10-2020 01:52 PM

I know of this command to play a random video from a folder:

Code:

mplayer -fs -quiet -shuffle {*,*/*}
I wonder if it's possible to append more code to also play a video at a random place in its run time?

individual 07-10-2020 02:30 PM

If you aren't currently using a particular video player, you could use mpv and some shell/Perl magic.
Code:

mpv --start="$(( RANDOM % 100 ))%" "$(perl -e '@d=<*>; print $d[rand(@d)]')"
A cleaner version:
Code:

#!/usr/bin/env perl

my @files = <*>;
my $start_percent = rand(100) . '%';

`mpv --start=$start_percent $files[rand(@files)]`


linustalman 07-11-2020 01:46 AM

Quote:

Originally Posted by individual (Post 6143893)
If you aren't currently using a particular video player, you could use mpv and some shell/Perl magic.
Code:

mpv --start="$(( RANDOM % 100 ))%" "$(perl -e '@d=<*>; print $d[rand(@d)]')"
A cleaner version:
Code:

#!/usr/bin/env perl

my @files = <*>;
my $start_percent = rand(100) . '%';

`mpv --start=$start_percent $files[rand(@files)]`


That one liner is perfect! Thanks, individual.

linustalman 07-11-2020 04:03 AM

Quote:

Originally Posted by individual (Post 6143893)
If you aren't currently using a particular video player, you could use mpv and some shell/Perl magic.
Code:

mpv --start="$(( RANDOM % 100 ))%" "$(perl -e '@d=<*>; print $d[rand(@d)]')"
A cleaner version:
Code:

#!/usr/bin/env perl

my @files = <*>;
my $start_percent = rand(100) . '%';

`mpv --start=$start_percent $files[rand(@files)]`


I tried the longer code and put it in a .pl file but it didn't work.

How can I add the one line command to an alias? I'm unsure due to the quotes already in the code.

Also, would it be possible to have the one line code run recursively?

individual 07-11-2020 06:14 PM

Quote:

Originally Posted by linustalman (Post 6144081)
I tried the longer code and put a .pl file but it did not work.

How can I add the one line command to an alias? I'm unsure due to the quotes already in the code.

Also, would it be possible to have the one line code run recursively?

It might be better to use a shell function instead of an alias.
Code:

randomFile() {
  ONE_LINER_GOES_HERE
}

I assume by recursively you mean you want to play a file that is several directories down? It would take a little more code, and probably not be a one-liner. If you need it, I can write something.

linustalman 07-12-2020 04:05 AM

Quote:

Originally Posted by individual (Post 6144366)
It might be better to use a shell function instead of an alias.
Code:

randomFile() {
  ONE_LINER_GOES_HERE
}

I assume by recursively you mean you want to play a file that is several directories down? It would take a little more code, and probably not be a one-liner. If you need it, I can write something.

That function is ideal. I will settle with the non-recursive setup. Thank you very much, individual. I appreciate it. :)


All times are GMT -5. The time now is 03:52 AM.