You can launch mpv from Linux and load all videos in a folder into its playlist using a few methods. Here's a breakdown with explanations and examples:
Method 1: Using Shell Expansion (Simplest for a single folder)
This is the most straightforward method when you just want to load videos from one specific directory.
mpv /path/to/your/video/folder/*.(mp4|mkv|avi|webm)
mpv: This is the command to launch the mpv media player.
- /path/to/your/video/folder/: Replace this with the actual path to the folder containing your videos. For example,
/home/user/Videos or . (if you're already in the video folder).
*.(mp4|mkv|avi|webm): This is the crucial part.
*: This is a wildcard character that matches any filename.
.(mp4|mkv|avi|webm): This specifies the file extensions you want to include. The | symbol means "or," so this pattern matches files ending in .mp4, .mkv, .avi, or .webm. You can add more extensions as needed. Be careful to include extensions that actually exist in your folder.
- Important Note: Shell expansion (the
*) is handled before mpv even runs. The shell expands the wildcard into a list of all matching files, and then that list is passed as arguments to mpv. If there are a lot of files, this could hit the maximum argument length limit, but that's rare.
Example:
If your videos are in /home/john/movies and you want to load MP4 and MKV files:
mpv /home/john/movies/*.(mp4|mkv)
Method 2: Using find (More Robust, Handles Subdirectories and Complex Cases)
This method uses the find command for more advanced filtering and handling of subdirectories. It's more robust when you have a complex directory structure or need more specific filtering.
find /path/to/your/video/folder -maxdepth 1 -type f \( -name "*.mp4" -o -name "*.mkv" -o -name "*.avi" -o -name "*.webm" \) -print0 | xargs -0 mpv
find /path/to/your/video/folder: Starts the find command, searching in the specified directory.
-maxdepth 1: This limits the search to only the immediate folder, preventing it from recursing into subdirectories. Remove this option if you want to include videos in subfolders.
-type f: Only finds regular files (not directories or other special file types).
\( -name "*.mp4" -o -name "*.mkv" -o -name "*.avi" -o -name "*.webm" \): This is the filtering part:
-name "*.mp4": Matches files ending in ".mp4". Replace with your desired extensions.
-o: Means "or". So, it matches files ending in ".mp4" or ".mkv" or ".avi" or ".webm". Add more -o -name "*.extension" pairs as needed.
\( and \): These are parentheses that group the -name and -o options. They need to be escaped with backslashes (\) to prevent the shell from interpreting them.
-print0: Prints the filenames separated by null characters. This is important when dealing with filenames that contain spaces or special characters.
|: This is a pipe. It takes the output of the find command and sends it as input to the next command.
xargs -0 mpv: xargs takes the input from find (the list of filenames) and executes the mpv command with those filenames as arguments.
-0: This tells xargs that the filenames are separated by null characters (as outputted by find -print0). This handles spaces and special characters in filenames correctly.
mpv: The command to execute (the mpv media player).
Example:
To find all MP4, MKV, and AVI files in /home/john/movies and load them into mpv:
find /home/john/movies -maxdepth 1 -type f \( -name "*.mp4" -o -name "*.mkv" -o -name "*.avi" \) -print0 | xargs -0 mpv
Method 3: Creating an M3U Playlist File (For persistent playlists)
This method creates a plain text playlist file (.m3u or .m3u8) that you can load into mpv or other media players. This is useful if you want to save a specific playlist for later use.
find /path/to/your/video/folder -maxdepth 1 -type f \( -name "*.mp4" -o -name "*.mkv" -o -name "*.avi" -o -name "*.webm" \) -print | sed 's|^|file://|' > playlist.m3u
mpv playlist.m3u
- The
find command is the same as in Method 2 (but uses -print instead of -print0).
-print: Prints the filenames, separated by newline characters. This is simpler than -print0 but may cause problems if filenames contain newlines.
| sed 's|^|file://|': This pipes the output of find to the sed command. sed is a stream editor that modifies the text.
s|^|file://|: This is a substitution command.
s: Indicates a substitution operation.
^: Matches the beginning of the line.
file://: The text to insert at the beginning of the line. The file:// prefix is often required for M3U playlists to specify that the files are local. If your mpv configuration is different, you might not need this prefix.
| : Separates the parts of the substitution command.
> playlist.m3u: This redirects the output of the sed command to a file named playlist.m3u. This creates the playlist file. You can use playlist.m3u8 for UTF-8 encoded playlists, which are recommended for broader compatibility.
mpv playlist.m3u: This launches mpv and tells it to load the playlist.m3u file.
Example:
find /home/john/movies -maxdepth 1 -type f \( -name "*.mp4" -o -name "*.mkv" \) -print | sed 's|^|file://|' > playlist.m3u
mpv playlist.m3u
Explanation of file:// prefix:
The file:// prefix is used in M3U playlists to explicitly tell the media player that the entries are local files accessed via the file system. While mpv often works fine without it, it's generally a good practice to include it for consistency and compatibility with other media players.
Which Method to Use:
- Method 1 (Shell Expansion): Best for simple cases when you just want to load videos from a single folder and you know the file extensions. Easiest to type.
- Method 2 (
find): More robust and flexible. Use this when you need to handle subdirectories (remove -maxdepth 1), have complex filtering requirements, or filenames might contain spaces or special characters.
- Method 3 (M3U Playlist): Use this when you want to create a persistent playlist that you can reuse later. Also useful if you want to edit the playlist manually.
Important Considerations:
- File Extensions: Double-check that the file extensions you're using (
mp4, mkv, avi, webm, etc.) are actually present in your video folder.
- Paths: Ensure the paths to your video folder are correct. Use absolute paths (starting with
/) for clarity and to avoid ambiguity. If you're in the video folder already, you can use . as the path.
- Permissions: Make sure you have read permissions for the video files and execute permissions for the folder.
- Subdirectories: If you want to include videos in subdirectories, remove the
-maxdepth 1 option from the find command. Be careful, as this could load a large number of files.
- Large Number of Files: While rare, if you have a truly massive number of files, the shell expansion or
xargs approach might hit argument length limits. If this happens, you might need to resort to more complex scripting solutions (e.g., writing a small Python script to generate the playlist and pass it to mpv).
Example Script (Python, for handling very large directories):
If you're dealing with thousands of files, and encounter argument length issues, here's a simple Python script as an alternative. This avoids the limitations of shell expansion and xargs:
import os
import subprocess
def find_videos(directory, extensions=['.mp4', '.mkv', '.avi', '.webm']):
"""Finds video files in a directory and its subdirectories."""
video_files = []
for root, _, files in os.walk(directory):
for file in files:
if any(file.endswith(ext) for ext in extensions):
video_files.append(os.path.join(root, file))
return video_files
if __name__ == "__main__":
video_dir = "/path/to/your/video/folder" # Replace with your directory
videos = find_videos(video_dir)
if videos:
subprocess.run(["mpv"] + videos)
else:
print("No video files found in", video_dir)
- Save: Save this code as a Python file (e.g.,
play_videos.py).
- Make Executable:
chmod +x play_videos.py
- Run:
./play_videos.py
This script is significantly more robust for extremely large directories than the shell-based solutions. It directly calls subprocess.run which is designed to handle many arguments. You can customize the file extensions list as needed. This avoids argument length limitations of the shell or xargs. The os.walk function allows it to handle subdirectories.