Create M3U Playlists in Linux Terminal to play files in a specific order [Quick Tip]
Brief: Quick tip on how to create M3U playlists in Linux terminal from unordered files to play in sequence.
I’m a fan of foreign TV series and it’s not always easy to get them on DVD or on streaming services like Netflix. Fortunately, you can find some of them on YouTube and download them from YouTube.
Now there is a problem. Your files may not be sorted in a specific order. In GNU/Linux files are not normally arranged in number sequence so I had to create a .m3u playlist so that the MPV video player would play the videos in sequence and not out of sequence.
Also sometimes the numbers are in the middle or at the end like “My Web Series S01E01.mkv” as an example. The episode info is right here in the middle of the file name, “S01E01” which tells us, mortals, which episode is first and which should come next.
So what I did is create an m3u playlist in the video directory and tell MPV to play the .m3u playlist and it will play them in sequence.
What is an m3u file?
M3U It is basically a text file that contains file names in a specific order. When a player such as MPV or VLC opens an M3U file, it attempts to play the specified files in the specified sequence.
Create M3U to play audio/video files in sequence
In my case, I used the following command:
$/home/shirish/Videos/web-series-video/$ ls -1v |grep .mkv > /tmp/1.m3u && mv /tmp/1.m3u .
Let’s break it down a bit and see each bit for what it means –
ll -1v = This command uses the normal ls or list the entries in the directory. -1 means to list one file per line. while -v is a natural type of (version) number within the text
| grep.mkv = She basically tells us ls
Finds files that end in .mkv. It can be .mp4 or any other media file format you want.
It’s usually a good idea to do a dry run by running the command on the console:
ls -1v |grep .mkv
My Web Series S01E01 [Episode 1 Name] Multi 480p WEBRip x264 - xRG.mkv
My Web Series S01E02 [Episode 2 Name] Multi 480p WEBRip x264 - xRG.mkv
My Web Series S01E03 [Episode 3 Name] Multi 480p WEBRip x264 - xRG.mkv
My Web Series S01E04 [Episode 4 Name] Multi 480p WEBRip x264 - xRG.mkv
My Web Series S01E05 [Episode 5 Name] Multi 480p WEBRip x264 - xRG.mkv
My Web Series S01E06 [Episode 6 Name] Multi 480p WEBRip x264 - xRG.mkv
My Web Series S01E07 [Episode 7 Name] Multi 480p WEBRip x264 - xRG.mkv
My Web Series S01E08 [Episode 8 Name] Multi 480p WEBRip x264 - xRG.mkv
This tells me that what I’m trying to do is correct. Only now you should make the output in the form of a .m3u playlist which is the next part.
ls -1v |grep .mkv > /tmp/web_playlist.m3u && mv /tmp/web_playlist.m3u .
This makes .m3u to be generated in the current directory. The .m3u playlist is nothing but a .txt file with the same contents as above with .m3u extension. You can edit it manually as well and add the exact file names in the order you want.
Then you just have to do something like this:
mpv web_playlist.m3u
The great thing about MPV and playlists, in general, is that you don’t have to binge-watch. You can see anything you want to do in one session and see the rest in the next session or session after that.
I’m hoping to make articles featuring MPV as well as how to create mkv files that include subtitles in a media file but that’s in the future.
Note: It is free and open source software that does not encourage piracy.