You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
localexcluded=false--Excluded directories (Also excludes subdirectories)localexcludes= {
'/mnt/storage/excluded/folder',
'C:\excluded\folder' --might need double backslashes to work
}
localfunctionexclude()
localpath, _=utils.split_path(utils.join_path(mp.get_property('working-directory'), mp.get_property('path')))
for_, exclusioninpairs(excludes) doifstring.find(path, exclusion) thenexcluded=trueendendend--the above are entirely new lines which can be placed pretty much anywhere, below are edits to existing lines or lines that must be placed at a specific location.localfunctionshutdown()
ifo.auto_saveandnotexcludedthen--this is the line that changedsave_playlist()
endendmp.register_script_message('save-session', save_playlist)
mp.register_script_message('reload-session', load_prev_session)
mp.register_event('file-loaded', exclude) --this is the line that was addedmp.register_event('shutdown', shutdown)
Whole file
--[[ This script automatically saves the current playlist and can reload it if the player is started in idle mode (specifically if there are 0 files in the playlist), or if the correct command is sent via script-messages. It remembers the playlist position the player was in when shutdown and reloads the playlist at that entry. This can be disabled with script-opts The script saves a text file containing the previous session playlist in the watch_later directory (changeable via opts) This file is saved in plaintext with the exact file paths of each playlist entry. Note that since it uses the same file, only the latest mpv window to be closed will be saved The script attempts to correct relative playlist paths using the utils.join_path function. I've tried to automatically detect when any non-files are loaded (if it has the sequence :// in the path), so that it'll work with URLs You can disable the automatic stuff and use script messages to load/save playlists as well script-message save-session [session-file] script-message reload-session [session-file] [load_playlist] If not included `session-file` will use the default file specified in script-opts. `load_playlist` controls whether the whole playlist should be restored or just the one file, the value can be `yes` or `no`. If not included it defaults to the value of the `load_playlist` script opt. available at: https://github.com/CogentRedTester/mpv-scripts]]--localmp=require'mp'localutils=require'mp.utils'localopt=require'mp.options'localmsg=require'mp.msg'localexcluded=falselocalo= {
--automatically save the prev sessionauto_save=true,
--runs the script automatically when started in idle mode and no files are in the playlistauto_load=true,
--reloads the full playlist from the previous session--can be individually overwritten when sending script-messagesload_playlist=true,
--file path of the default session file--save it as a .pls file to be able to open directly (though it will not maintain the playlist positions)session_file="",
--maintain position in the playlist--does nothing if load_playlist is disabledmaintain_pos=true,
}
--Excluded directories (Also excludes subdirectories)localexcludes= {
'/mnt/storage/excluded/folder',
'C:\excluded\folder'
}
localfunctionexclude()
localpath, _=utils.split_path(utils.join_path(mp.get_property('working-directory'), mp.get_property('path')))
for_, exclusioninpairs(excludes) doifstring.find(path, exclusion) thenexcluded=trueendendendopt.read_options(o, 'keep_session', function() end)
--sets the default session file to the watch_later directory or ~~/watch_later/ifo.session_file=="" thenlocalwatch_later=mp.get_property('watch-later-directory', "")
ifwatch_later=="" thenwatch_later="~~state/watch_later/" endifnotwatch_later:find("[/\\]$") thenwatch_later=watch_later..'/' endo.session_file=watch_later.."prev-session"endlocalsave_file=mp.command_native({"expand-path", o.session_file})
--saves the current playlist as a json stringlocalfunctionsave_playlist(file)
ifnotfilethenfile=save_fileendmsg.verbose('saving current session to', file)
localplaylist=mp.get_property_native('playlist')
if#playlist==0thenmsg.verbose('session empty, aborting save')
returnendlocalsession=io.open(file, 'w')
ifnotsessionthenreturnmsg.error("Failed to write to file", file) endsession:write("[playlist]\n")
session:write(mp.get_property('playlist-pos') .."\n")
localworking_directory=mp.get_property('working-directory')
for_, vinipairs(playlist) domsg.debug('adding ' ..v.filename..' to playlist')
--if the file is available then it attempts to expand the path in-case of relative playlists--presumably if the file contains a protocol then it shouldn't be expandedifnotv.filename:find("^%a*://") thenv.filename=utils.join_path(working_directory, v.filename)
msg.debug('expanded path: ' ..v.filename)
endsession:write("File=" ..v.filename.."\n")
endsession:close()
end--turns the previous json string into a table and adds all the files to the playlistlocalfunctionload_prev_session(file, load_playlist)
ifnotfileorfile=='' thenfile=save_fileendifload_playlist=='yes' thenload_playlist=trueelseifload_playlist=='no' thenload_playlist=falseelseload_playlist=o.load_playlistend--loads the previous session filemsg.verbose('loading previous session from', file)
localsession=io.open(file, "r+")
--this should only occur when loading the script for the first time,--or if someone manually deletes the previous session fileifsession==nilorsession:read() ~="[playlist]" thenmsg.verbose('no previous session, cancelling load')
ifsessionthensession:close() endreturnendlocalprevious_playlist_pos=session:read('*n')
ifload_playlistthenmsg.debug('reloading playlist')
ifnoto.maintain_posthenmp.commandv('loadlist', file)
elselocalprev_playlist_start=mp.get_property('playlist-start')
msg.verbose("restoring playlist position", previous_playlist_pos)
mp.set_property_number('playlist-start', previous_playlist_pos)
mp.commandv('loadlist', file)
-- restore the original value unless the `playlist-start` property has been otherwise modifiedifmp.get_property_number('playlist-start') ~=previous_playlist_posthenmp.set_property('playlist-start', prev_playlist_start)
endendelsemsg.debug('discarding playlist')
localfiles= {}
forlineinsession:lines() dotable.insert(files, string.match(line, 'File=(.+)'))
end-- mpv and keep-session uses 0 based array indices, but lua uses 1-basedmp.commandv('loadfile', files[previous_playlist_pos+1])
endsession:close()
endlocalfunctionshutdown()
ifo.auto_saveandnotexcludedthensave_playlist()
endendmp.register_script_message('save-session', save_playlist)
mp.register_script_message('reload-session', load_prev_session)
mp.register_event('file-loaded', exclude)
mp.register_event('shutdown', shutdown)
--Load the previous session if auto_load is enabled and the playlist is empty--the function is not called until the first property observation is triggered to let everything initialise--otherwise modifying playlist-start becomes unreliableifo.auto_loadand (mp.get_property_number('playlist-count', 0) ==0) thenlocalfunctiontemp()
load_prev_session()
mp.unobserve_property(temp)
endmp.observe_property("idle", "string", temp)
end
I'm too lazy to make a PR but it would be cool if this, or a version of this were included in the official file.
The text was updated successfully, but these errors were encountered:
I already created my own version of this:
Relevant code:
Whole file
I'm too lazy to make a PR but it would be cool if this, or a version of this were included in the official file.
The text was updated successfully, but these errors were encountered: