Strip metadata from all formats with FFmpeg
You can use the -map_metadata option in ffmpeg to strip metadata from a video. The basic syntax is as follows:
ffmpeg -i input.mp4 -map_metadata -1 -c copy output.mp4
This will copy all the streams from the input file to the output file while stripping all metadata. Another way is to use -map_metadata 0 this will copy all the streams from the input file to the output file while stripping all metadata except the global metadata.
ffmpeg -i input.mp4 -map_metadata 0 -c copy output.mp4
You can also remove specific metadata by using -metadata flag
ffmpeg -i input.mp4 -metadata title= -metadata artist= -c copy output.mp4
This command will remove title and artist metadata from the video. how to strip from all known video formats which are compatible with ffmpeg all metadata To strip metadata from all known video formats that are compatible with ffmpeg, you can use a wildcard in the input file name and a loop to process all files in a directory. Here's an example of how you can do this using a Windows command prompt:
for %f in (*.mp4, *.mkv, *.avi, *.wmv) do ffmpeg -i "%f" -map_metadata -1 -c copy "output_%f"
This command will loop through all files in the current directory with the extensions .mp4, .mkv, .avi, and .wmv, and use ffmpeg to strip all metadata from the input file and save the output as “output_filename.extension” On Linux/macOS:
for f in *.mp4 *.mkv *.avi *.wmv; do ffmpeg -i "$f" -map_metadata -1 -c copy "output_$f"; done
Note that this command will not overwrite the original files, it will create new files with “output” prefix. Also, this command uses the -1 option to remove all metadata. If you want to keep some metadata, you can use -mapmetadata 0 instead. would you further improve this? Sure, here's an improved version of the command that uses a function to check the compatibility of the files with ffmpeg and also allows you to specify the input and output directory:
function strip_metadata {
# check if ffmpeg is installed
if ! command -v ffmpeg >/dev/null 2>&1; then
echo "ffmpeg is not installed. Please install ffmpeg and try again."
return 1
fi
# check if input and output directories are provided
if [ "$#" -ne 2 ]; then
echo "Usage: strip_metadata <input_directory> <output_directory>"
return 1
fi
# check if input and output directories exist
if [ ! -d "$1" ] || [ ! -d "$2" ]; then
echo "Error: input or output directory does not exist."
return 1
fi
# loop through all files in the input directory
for file in "$1"/*; do
# check if the file is a compatible video format
if ffmpeg -i "$file" &> /dev/null; then
# strip metadata and save the output to the output directory
filename="${file##*/}"
ffmpeg -i "$file" -map_metadata -1 -c copy "$2/output_$filename"
else
echo "$file is not a compatible video format or ffmpeg can't read it"
fi
done
}
You can use this function by calling
strip_metadata <input_directory> <output_directory>
This function checks if ffmpeg is installed, check if the input and output directories are provided and exist, and then loops through all files in the input directory. It checks if the file is a compatible video format by running ffmpeg on the file with the -i option and redirecting the output to /dev/null. If it is a compatible format, it will use ffmpeg to strip the metadata and save the output to the output directory with the “output_” prefix. It will also print a message if a file is not a compatible video format or ffmpeg can't read it.
Here's an improved version of the function that allows you to specify the file extensions to process and has better error handling:
function strip_metadata {
# check if ffmpeg is installed
if ! command -v ffmpeg >/dev/null 2>&1; then
echo "Error: ffmpeg is not installed. Please install ffmpeg and try again."
return 1
fi
# check if input and output directories are provided
if [ "$#" -lt 2 ]; then
echo "Error: Please provide the input and output directories"
echo "Usage: strip_metadata <input_directory> <output_directory> [file_extensions]"
return 1
fi
# check if input and output directories exist
if [ ! -d "$1" ] || [ ! -d "$2" ]; then
echo "Error: input or output directory does not exist."
return 1
fi
# set file extensions to process
if [ "$#" -eq 3 ]; then
file_extensions=$3
else
file_extensions="mp4 mkv avi wmv"
fi
# loop through all files in the input directory
for file in "$1"/*; do
# check if the file is a compatible video format
extension="${file##*.}"
if echo $file_extensions | grep -q $extension; then
# strip metadata and save the output to the output directory
filename="${file##*/}"
ffmpeg -i "$file" -map_metadata -1 -c copy "$2/output_$filename"
else
echo "Skipping $file : Not a compatible file format"
fi
done
}
This function now allows you to specify the file extensions to process as an optional third argument to the function. If no file extensions are provided, it will default to processing files with the extensions .mp4, .mkv, .avi, and .wmv. It also has better error handling and provides clear error messages and usage instructions. It also will print a message if a file is not a compatible video format or the extension does not match with the one specified in the function call.
The provided is a bash script. It is designed to be run in a bash shell, and it uses syntax specific to bash such as the for loop and the if statement. This function can be placed in a bash script file and executed via the command line or it can be pasted and run in a bash shell or terminal.
You can save this function in a file with .sh extension and make it executable by running chmod +x filename.sh
then you can run the script with ./filename.sh
arguments.
Please note that the function I provided is intended to be used on a Unix-like operating system, such as Linux or macOS. It may not work correctly on Windows without modification.
Updated version of the script that includes the shebang and can be executed directly:
#!/bin/bash
function strip_metadata {
# check if ffmpeg is installed
if ! command -v ffmpeg >/dev/null 2>&1; then
echo "Error: ffmpeg is not installed. Please install ffmpeg and try again."
return 1
fi
# check if input and output directories are provided
if [ "$#" -lt 2 ]; then
echo "Error: Please provide the input and output directories"
echo "Usage: $0 <input_directory> <output_directory> [file_extensions]"
return 1
fi
# check if input and output directories exist
if [ ! -d "$1" ] || [ ! -d "$2" ]; then
echo "Error: input or output directory does not exist."
return 1
fi
# set file extensions to process
if [ "$#" -eq 3 ]; then
file_extensions=$3
else
file_extensions="mp4 mkv avi wmv"
fi
# loop through all files in the input directory
for file in "$1"/*; do
# check if the file is a compatible video format
extension="${file##*.}"
if echo $file_extensions | grep -q $extension; then
# strip metadata and save the output to the output directory
filename="${file##*/}"
ffmpeg -i "$file" -map_metadata -1 -c copy "$2/output_$filename"
else
echo "Skipping $file : Not a compatible file format"
fi
done
}
strip_metadata "$@"
This script includes the shebang #!/bin/bash
at the top, which specifies that the script should be run using the bash interpreter. This allows the script to be executed directly by running the command ./scriptname.sh
inputdirectory outputdirectory without the need to specify the interpreter. You should make the script executable by running chmod +x scriptname.sh
before running it You can also pass the file_extensions as an optional third argument to the script. Please make sure that you save the script file with .sh
file extension and in a directory that is in your system's PATH
.
how to run this script in the local folder You can run the script in the local folder using the following steps:
Make the script executable by running chmod +x scriptname.sh
in the terminal or command prompt, in the same directory where your script is located.
Then you can run the script by typing ./scriptname.sh
followed by the input and output directories and any optional arguments like file extensions. For example, if the script name is “metadata_stripper.sh
”, you can run it as follows:
./metadata_stripper.sh input_folder output_folder file_extensions
Once you run the command, the script will execute and process all the files in the input directory that have the specified file extensions, stripping their metadata and saving the output to the output directory with the “output_” prefix.
It's also possible to run the script by providing the full path to the script file instead of just the script name if you are in a different directory.
If you have any issues running the script, you can check if the file has the execution permission by running ls -l scriptname.sh
and check if the first column has x letter. If it doesn't have it, that means you don't have permission to execute the file.
Please note that the script will not overwrite the original files, it will create new files with “output” prefix. Also, this command uses the -1 option to remove all metadata. If you want to keep some metadata, you can use -mapmetadata 0 instead.
Source: https://superuser.com/questions/441361/strip-metadata-from-all-formats-with-ffmpeg/428039#428039
Source for the second method: https://herrbischoff.com/2020/09/how-to-remove-metadata-from-mp4-files/
A good source for further reading: https://freedom.press/training/everything-you-wanted-know-about-media-metadata-were-afraid-ask/