This is an old revision of the document!
Table of Contents
VLC Playlist from file
These scripts make VLC behave like MPC-HC, ie when you open a file with VLC in a folder, it will automatically add all the files in this folder to the playlist (starting with the opened file, then sorted alphabetically), so that you can use Previous and Next to navigate through the folder.
https://github.com/x-hgg-x/Utils/tree/master/VLC%20Create%20playlist%20from%20file
Usage
Notes
Due to internal behaviour of VLC, you cannot replace current playlist by another, only append it. Therefore the script must kill existing instances of VLC to launch a new playlist, so it works only when VLC is in one-instance mode.
You may want to activate the loop option in VLC preferences if VLC is configured to play the next track automatically. (Tools → Preferences → Show all settings → Playlist → Repeat current item)
Linux
Copy vlc.sh in ~/.local/bin directory and mark it executable, then copy the desktop file of VLC /usr/share/applications/vlc.desktop to ~/.local/share/applications/vlc.desktop and change the field Exec to the path of vlc.sh (remove argument –started-from-file %U). Known extensions are stocked in variable $extensions.
- ~/.local/bin/vlc.sh
#!/bin/bash shopt -s extglob extensions='@(avi|mp4|mkv|m4v|mov|mpg|mpeg|wmv|ogg|flac|m4a|mp3|wav)' # list of extensions for searching in current directory # kill other instances of vlc to keep playlist clean (one-instance mode) killall vlc; sleep 0.1 # launch empty vlc if no argument provided if [ -z "$1" ]; then vlc; exit fi # parse argument filename=$(realpath -- "$1") dirname=$(dirname "$filename") basename=$(basename "$filename") # count files with matching extension, and get position of filename in current directory n=$(ls "${dirname}"/*.${extensions} -1 2>/dev/null | wc -l) pos=$(ls "${dirname}"/*.${extensions} -1 2>/dev/null | grep -n -F -- "${basename}" | cut -d: -f1) # if the filename does not have one of the extension above, launch vlc with provided filename if [ -z "$pos" ]; then vlc -- "${filename}" exit fi # change positions in playlist such as the first element is the opened file ls "${dirname}"/*.${extensions} -1 | tail -n$(($n-$pos+1)) > /tmp/vlc.m3u ls "${dirname}"/*.${extensions} -1 | head -n$(($pos-1)) >> /tmp/vlc.m3u # launch playlist IFS=$'\n'; read -d '' -r -a files < /tmp/vlc.m3u; vlc "${files[@]}"
chmod 755 ~/.local/bin/vlc.sh sed "s#Exec=/usr/bin/vlc --started-from-file %U#Exec=/home/$USER/.local/bin/vlc.sh#g" /usr/share/applications/vlc.desktop > ~/.local/share/applications/vlc.desktop
Windows
Copy vlc.ps1 and vlc.bat in your home directory, then you can open your video/audio file with vlc.bat like this :
vlc.bat video.mp4.
You may want to run Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy UNRESTRICTED once in a powershell terminal to allow exectution of powershell scripts.
You can check if the variable $vlcPath is correct in vlc.ps1, and modify known extensions with the variable $Extensions.
- vlc.bat
@echo off powershell -File %USERPROFILE%\vlc.ps1 %1
- vlc.ps1
Param([string]$filename) # kill other instances of vlc to keep playlist clean (one-instance mode) Stop-Process -Name vlc -ErrorAction SilentlyContinue $vlcPath = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" $Extensions='(\.avi$|\.mp4$|\.mkv$|\.m4v$|\.mov$|\.mpg$|\.mpeg$|\.wmv$|\.ogg$|\.flac$|\.m4a$|\.mp3$|\.wav$)' if ([string]::IsNullOrEmpty($filename)) {& $vlcPath; exit} # launch empty vlc if no argument provided # parse argument $filename = Resolve-Path -LiteralPath $filename $dirname = Split-Path -LiteralPath $filename $basename = Get-ChildItem -LiteralPath $filename | ForEach-Object { $_.Name } # count files with matching extension, and get position of filename in current directory $filelist = Get-ChildItem -File -LiteralPath $dirname | ForEach-Object { $_.Name } | Select-String -Pattern $Extensions | ForEach-Object { $_.Line } $count = $($filelist | Measure-Object -Line).Lines $position = $filelist | Select-String -Pattern $basename | ForEach-Object { $_.LineNumber } # if the filename does not have one of the extension above, launch vlc with provided filename if ([string]::IsNullOrEmpty($position)) {& $vlcPath $filename; exit} # change positions in playlist such as the first element is the opened file $filelist | Select-Object -last ($count-$position+1) | ForEach-Object { "$dirname\" + $_ } > $Env:TMP\vlc.m3u $filelist | Select-Object -first ($position-1) | ForEach-Object { "$dirname\" + $_ } >> $Env:TMP\vlc.m3u # launch playlist & $vlcPath $(Get-Content $Env:TMP\vlc.m3u)