How do I modify my FFmpeg script in a batch file so that the output file Video.Mkv is directed to a folder named “FIXED” located in the same directory as the script?
Mk Fixed
for /R %%f IN (*.mkv) do ffmpeg -i "%%f" -map 0:m:language=English? -c copy "%%~nf.MKV"
At present, the script searches the main folder and its subfolders for Mkv files and retains only the English audio track. However, it saves all the Mkv files in the same directory as the script instead of the designated “Fixed” folder.
Although I have another script that meets my requirements, I was unable to make it work for recursively scanning subfolders during batch scanning. This alternative script directs the output to the appropriate folder.
Mk Fixed
for %%a in ("*mkv*") do ffmpeg -i "%%a" -map 0:0 -c:v copy -map 0:3 -c:a copy "Fixed\%%~na".mkv
When I attempt to include “fixed%%~nf.Mkv” in my initial script, I encounter a problem.
[matroska,webm @ 0000018fb8518840] File ended prematurely
It would be excellent if the script could retain the directory structure from the source to the destination folder in the output.
— Response: I have made changes to my script to accommodate this.
mkdir Fixed
for /R %%f IN (*.mkv) do ffmpeg -i "%%f" -map 0:m:language=English? -c copy "Fixed\%%~nf.mkv"
Although it is functional, the output still does not preserve the structure of subfolders. I want FFMpeg to scan through the subdirectories and convert the video files while preserving the folder structure.
I should have provided more information earlier. My operating system is Windows 10 Pro, and the directory structure is as follows: C:\Users\User\Desktop\Convert
(contains the files that need to be converted), the folder where FFMPEG and the script.bat file are located, as well as subfolders containing files that require conversion.
Example C:\Users\User\Desktop\Convert\Tv Show\Show 1\Season 1\Show.mkv
Goal C:\Users\User\Desktop\Convert\Fixed\Tv Show\Show 1\Season 1\Show.mkv
3 Answers
Introduction
FFmpeg is a powerful command-line tool that can be used to convert multimedia files. It is an open-source software that can be used on various platforms like Windows, Linux, and Mac. FFmpeg can convert audio and video files, change their format, and perform various other operations. In this blog post, we will discuss how to modify an FFmpeg script in a batch file to output the video.mkv to a folder called “FIXED” located in the same directory as the script.
Current FFmpeg Script
The current FFmpeg script searches the main folder and its subfolders for Mkv files and retains only the English audio track. However, it saves all the Mkv files in the same directory as the script instead of the designated “Fixed” folder.
Mk Fixed
for /R %%f IN (*.mkv) do ffmpeg -i "%%f" -map 0:m:language=English? -c copy "%%~nf.MKV"
The Problem with the Current Script
The problem with the current FFmpeg script is that it saves all the Mkv files in the same directory as the script instead of the designated “Fixed” folder. Although this script searches the main folder and its subfolders for Mkv files and retains only the English audio track, it does not save the output files in the desired location.
Alternative FFmpeg Script
Although there is another script that meets the requirements, it does not work for recursively scanning subfolders during batch scanning. This alternative script directs the output to the appropriate folder.
Mk Fixed
for %%a in ("*mkv*") do ffmpeg -i "%%a" -map 0:0 -c:v copy -map 0:3 -c:a copy "Fixed%%~na".mkv
The Problem with the Alternative Script
The problem with this script is that it does not scan through the subdirectories and convert the video files while preserving the folder structure. It only converts the files in the current directory and saves the output files in the “Fixed” folder.
Modifying the FFmpeg Script
To modify the FFmpeg script in the batch file to output the video.mkv to a folder called “FIXED” located in the same directory as the script, we need to make some changes to the existing script. Here is the modified script:
mkdir Fixed
for /R %%f IN (*.mkv) do ffmpeg -i "%%f" -map 0:m:language=English? -c copy "Fixed%%~nf.mkv"
The changes made to the script are as follows:
– We have added the “mkdir Fixed” command to create the “Fixed” folder in the same directory as the script. This command creates the folder only if it does not already exist.
– We have replaced “%%~nf.MKV” with “Fixed%%~nf.mkv” to save the output files in the “Fixed” folder located in the same directory as the script.
– We have removed the question mark in “-map 0:m:language=English?” because it is not required.
Preserving Folder Structure
To preserve the folder structure while converting the video files, we need to make some more changes to the script. Here is the modified script:
@echo off
setlocal enabledelayedexpansion
set "source=C:UsersUserDesktopConvert"
set "destination=%~dp0Fixed"
for /R "%source%" %%f in (*.mkv) do (
set "folder=%%~dpf"
set "folder=!folder:%source%=!"
if not exist "%destination%!folder!" mkdir "%destination%!folder!"
ffmpeg -i "%%f" -map 0:m:language=English -c copy "%destination%!folder!%%~nxf"
)
The changes made to the script are as follows:
– We have added the “@echo off” command to turn off the command prompt display.
– We have added the “setlocal enabledelayedexpansion” command to enable delayed expansion of variables.
– We have added the “set” command to set the source and destination folders.
– We have used the “for /R” loop to traverse through the source folder and its subfolders to find the .mkv files.
– We have used the “set” command to set the “folder” variable to the current folder path.
– We have used the “set” command with the “!” syntax to enable delayed expansion of the “folder” variable.
– We have used the “if not exist” command to create the folder in the destination directory if it does not already exist.
– We have used the “ffmpeg” command to convert the video files and save them in the appropriate folder.
Explanation of the Modified Script
The modified script uses the “set” command to set the source and destination folders. The source folder is where the files that need to be converted are located, and the destination folder is where the converted files will be saved.
The “for /R” loop traverses through the source folder and its subfolders to find the .mkv files. The “set” command sets the “folder” variable to the current folder path, including the source folder.
The “set” command with the “!” syntax enables delayed expansion of the “folder” variable. The “if not exist” command checks if the folder exists in the destination directory. If it does not exist, the command creates the folder in the destination directory.
The “ffmpeg” command converts the video files and saves them in the appropriate folder. The “-map 0:m:language=English” command retains only the English audio track, and the “-c copy” command copies the video and audio streams without re-encoding them, which saves time and preserves the quality.
Conclusion
In conclusion, we have discussed how to modify an FFmpeg script in a batch file to output the video.mkv to a folder called “FIXED” located in the same directory as the script. We have seen the problems with the current and alternative scripts and made the necessary changes to the script to achieve the desired output. We have also discussed how to preserve the folder structure while converting the video files. The modified script is efficient and saves time while preserving the quality of the video and audio streams.
To output the converted MKV files to a subfolder called “Fixed” within the same directory as the script, you can use the following script:
mkdir Fixed
for /R %%f IN (*.mkv) do (
ffmpeg -i "%%f" -map 0:m:language=English? -c copy "Fixed\%%~pf%%~nf.mkv"
)
The %%~pf
variable expands to the full path of the file, including the drive letter and all subdirectories. The %%~nf
variable expands to the file name without the extension. By combining these two variables, the full path and file name of the original MKV file is preserved in the output file name.
If you also want to preserve the directory structure within the “Fixed” folder, you can use the following script instead:
for /R %%f IN (*.mkv) do (
mkdir "Fixed\%%~pf"
ffmpeg -i "%%f" -map 0:m:language=English? -c copy "Fixed\%%~pf%%~nf.mkv"
)
This script will create the necessary subdirectories under “Fixed” as needed, based on the path of the original MKV files.
I hope this helps! Let me know if you have any further questions.
In the end, I opted for a program called “FFmpeg Batch AV Converter” to achieve the desired outcome. This program enables me to execute my script while selecting the option to maintain the source folder structure in the output directory. The program has an option in the bottom right corner that allows for this.
Thank you for your assistance. Using only the bat file did not yield the expected result.