Skip to content

Optimizes cycle_tracks, fixes 2 bugs, adds cycling backwards #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2024
Merged

Optimizes cycle_tracks, fixes 2 bugs, adds cycling backwards #18

merged 1 commit into from
Jan 25, 2024

Conversation

kaoneko
Copy link
Contributor

@kaoneko kaoneko commented Jan 24, 2024

Summary:

  • reduced cycle-known-tracks to 27 lines of code
  • fixed 2 bugs
  • added cycling backwards

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 the track_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 of mode with the relevant strings to refer to the right options and properties, like include_no_**audio** or **a**lang. This way we don't need to be setting the lang_prop, id_prop and type_name variables. That also gets rid of the need (if there ever was one) to spread the functionality over two functions (the anonymous one and cycle_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 and track.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 the id_list in the same loop and we only have to go through the track_list once.

Also, the script would start cycling through only known language tracks upon reaching max_count (count < max_count) instead of exceeding max_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 in id_list, however this will always be false (since it's always set to alang or slang). I guess the intention was to include all tracks when alang or slang is not configured, and what should've been evaluated was prop or lang_list. So I did this with not next(lang_list), which returns true (not false) when lang_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!

@stax76 stax76 merged commit d846f31 into stax76:main Jan 25, 2024
@stax76
Copy link
Owner

stax76 commented Jan 25, 2024

Hi, yes I like it, thanks for contributing the code and for the detailed explanation. If you have a mpv-scripts repo you should include it there too.

I improved the documentation a bit:

cc18ffc

@kaoneko kaoneko deleted the patch-1 branch January 25, 2024 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants