Optimizes cycle_tracks, fixes 2 bugs, adds cycling backwards #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary:
Hey, I don't know if you take pull requests, but I found your script when searching for one that lets me cycle subtitle tracks in my known languages only and it was exactly what I needed. Upon reading the code I immediately thought it could probably be written more concise and efficiently, and over the course of a few days I got to work, learning a thing or two about mpv and LUA script in the process. I was able to reduce the needed code from 87 lines (incl. used helper functions, excl. blank lines) to 27 lines (no helper functions used). A few things contributed to this.
To start with, we can use the mpv command
cycle-values
so we particularly don't have to handle cycling around from the highest to the lowest track ourselves. This does mean we always have to include the current track in thetrack_list
so it knows where to start, so I added that. It also offers the possibility to easily reverse the direction of our cycling (just like the default configuration of mpv does when pressing J) so I added that as well, without breaking current input.conf configurations that don't specify a direction.We can concatenate
mode
or the first letter ofmode
with the relevant strings to refer to the right options and properties, likeinclude_no_**audio**
or**a**lang
. This way we don't need to be setting thelang_prop
,id_prop
andtype_name
variables. That also gets rid of the need (if there ever was one) to spread the functionality over two functions (the anonymous one andcycle_tracks(lang_prop, id_prop, type_name)
).By storing the languages as keys of a table we don't need to traverse the whole table to find out if a language is defined, we can just ask for the value of a key named
lang
, and it will return true if it's in the table, false/nil otherwise. This is probably more efficient and we also don't need a helper function to do it.The track list can be retrieved as a table, after which we can use things like
track.type
andtrack.lang
, making for readable code without the need to use variables to make it so.Finally, we can simply tell mpv to go to the next (or previous) track when the number of audio/subtitle tracks doesn't exceed
max_count
. This way we can handle counting and compiling theid_list
in the same loop and we only have to go through thetrack_list
once.Also, the script would start cycling through only known language tracks upon reaching
max_count
(count < max_count
) instead of exceedingmax_count
(count <= max_count
) so I fixed that.I'm not sure why a user of this script would want to select the next track in one of their known languages manually when there's a limited amount of tracks, so I think that functionality could even be removed, but maybe there's a use case that I'm not aware of.
Also removing spaces from the language list seems unnecessary to me, as it should be a comma separated list of two and three letter languages identifiers—no spaces there. But I didn't remove it of course, just wanted to make the script more efficient while offering the same functionality.
O, another thing:
lang_prop == ""
was evaluated to determine whether to include a track inid_list
, however this will always be false (since it's always set toalang
orslang
). I guess the intention was to include all tracks when alang or slang is not configured, and what should've been evaluated wasprop
orlang_list
. So I did this withnot next(lang_list)
, which returns true (not false
) whenlang_list
is empty.I had a lot of fun challenging myself to make this code as efficient as possible, and I'm quite proud of the result. Hope you like it too!