Brief: A quick tip on how to create M3U playlists in Linux terminal from unordered files to play them in a sequence.
I am a fan of foreign tv series and it’s not always easy to get them on DVD or on streaming services like Netflix. Thankfully, you can find some of them on YouTube and download them from YouTube.
Now there comes a problem. Your files might not be sorted in a particular order. In GNU/Linux files are not naturally sort ordered by number sequencing so I had to make a .m3u playlist so MPV video player would play the videos in sequence and not out of sequence.
Also sometimes the numbers are in the middle or the end like ‘My Web Series S01E01.mkv’ as an example. The episode information here is in the middle of the filename, the ‘S01E01’ which tells us, humans, which is the first episode and which needs to come in next.
So what I did was to generate an m3u playlist in the video directory and tell MPV to play the .m3u playlist and it would take care of playing them in the sequence.
What is an M3U file?
M3U is basically a text file that contains filenames in a specific order. When a player like MPV or VLC opens an M3U file, it tries to play the specified files in the given sequence.
Creating M3U to play audio/video files in a 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 as to what it means –
ls -1v = This is using the plain ls or listing entries in the directory. The -1 means list one file per line. while -v natural sort of (version) numbers within text
| grep .mkv = It’s basically telling ls
to look for files which are ending in .mkv . It could be .mp4 or any other media file format that 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. Now just have to make that the output is 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 the .m3u generate in the current directory. The .m3u playlist is nothing but a .txt file with the same contents as above with the .m3u extension. You can edit it manually as well and add the exact filenames in an order you desire.
After that you just have to do something like this:
mpv web_playlist.m3u
The great thing about MPV and the playlists, in general, is that you don’t have to binge-watch. You can see however much you want to do in one sitting and see the rest in the next session or the session after that.
I hope to do articles featuring MPV as well as how to make mkv files embedding subtitles in a media file but that’s in the future.
Note: It’s FOSS doesn’t encourage piracy.
whats the point of the
&& mv /tmp/web_playlist.m3u .
That usually is for when you have a playlist on the web, some specific singer or whatever. What will happen is mpv will keep track of where you are and you can close and use it again. One thing to note though, because it would be in /tmp if you reboot it would lose the sequence of the playlist, so only works if you are in the session.
I used this same command and my playlists would always show up as empty in my player interface. The issue was that the ‘ls’ command was applying color values to each file/line in my new .m3u file. Basically this is what tells the terminal/shell to display a list of files as green text for example. by doing ‘/ls’ instead, bash doesn’t use the alias of ‘ls’ which is what defines the color in the first place. the final command looks like /ls -1 *mp3 > yourplaylist.m3u
I’m sorry it didn’t work for you, it does and did work for me. One thing though, I did that in bash 4.x and now most distros. including Debian is using 5.x . It is possible that because something might have changed in the transition. I will have to check in a new profile to see if something has changed but that will take time.
Isn’t “ls -1v *.mkv” shorter and more efficient then “ls -1v | grep .mkv”?
true, but I use the one I shared above so I know what I’m actually doing and it’s also sort of understandable to others. Although if one knows basic regexp, then the usage you shared is more easier for sure.