Right now, I am trying to retrieve or access some files:
example.mkv // This is the source with audio/subs
example-Encoded.mkv // This is video only/encoded source
I have created a script that is functioning properly, but it encounters an error when attempting to retrieve additional files after successfully obtaining the first two. The script completes the remuxing process, but the error occurs because it is searching for a file named “example-Encoded-Encode.mkv”.
Below is the script I am currently utilizing:
mkdir MUXED
FOR %%a IN ("*.mkv") DO ffmpeg -i "%%a" -i "%%~na-Encoded.mkv" ^
-map 1:v:0 c:v:0 copy -disposition:v:0 default -metadata:s:v:0 title="Example" ^
-map 0:a:0 -c:a:0 copy -disposition:a:0 default -metadata:s:a:0 title="DD 2.0" ^
-map 0:a:1 -c:a:1 copy -metadata:s:s:0 title="Commentary" ^
-map 0:s:0 -c:s:0 copy -metadata:s:s:0 title="English (SRT)" ^
-map 0:s:1 -c:s:1 copy -metadata:s:s:1 title="English (VobSub)" ^
"MUXED\%%~na.mkv"
pause
I have the option of using the same file names by transforming the encoded video files into an mp4 container. Since the files are solely encoded video, this is achievable. Nonetheless, I have a sense that there might be a more suitable method to accomplish this without having to modify the containers beforehand.
2 Answers
Introduction
Batch files are scripts that allow you to automate repetitive tasks on your computer. They can be used to perform a wide variety of actions, such as copying files, renaming files, and executing programs. In this blog post, we will show you how to create a batch file that joins two files that have similar names and the same extensions. This can be useful if you have a video file and an audio file that you want to combine into a single file.
Understanding the Problem
Let’s start by understanding the problem. You have two files: “example.mkv” and “example-Encoded.mkv”. The first file contains both audio and subtitles, while the second file contains only video. You want to combine these two files into a single file, but you don’t want to do it manually every time. You want to create a batch file that automates the process.
The batch file should be able to recognize that the two files have similar names and the same extensions. It should then use FFmpeg to combine the two files into a single file. The resulting file should have the same name as the original video file (“example.mkv”), but with the audio and subtitles from the first file and the video from the second file.
Creating the Batch File
To create the batch file, open Notepad or any other text editor and paste the following code:
mkdir MUXED
FOR %%a IN ("*.mkv") DO ffmpeg -i "%%a" -i "%%~na-Encoded.mkv" ^
-map 1:v:0 c:v:0 copy -disposition:v:0 default -metadata:s:v:0 title="Example" ^
-map 0:a:0 -c:a:0 copy -disposition:a:0 default -metadata:s:a:0 title="DD 2.0" ^
-map 0:a:1 -c:a:1 copy -metadata:s:s:0 title="Commentary" ^
-map 0:s:0 -c:s:0 copy -metadata:s:s:0 title="English (SRT)" ^
-map 0:s:1 -c:s:1 copy -metadata:s:s:1 title="English (VobSub)" ^
"MUXED%%~na.mkv"
pause
Let’s break down the code. The first line creates a new directory called “MUXED” where the resulting file will be saved.
The second line uses a “FOR” loop to iterate through all the “.mkv” files in the current directory. The loop variable “%%a” represents each file that matches the pattern.
The third line runs FFmpeg to combine the two files. The “-i” option specifies the input files. The first input file is “%%a”, which represents the video with audio and subtitles. The second input file is “%%~na-Encoded.mkv”, which represents the encoded video file. The “~n” modifier removes the file extension from “%%a”, and the result is concatenated with “-Encoded.mkv”.
The next six lines specify the mapping of the input streams to the output streams. The “-map” option specifies the input stream to use for each output stream. The “c:v:0” option copies the first video stream from the second input file to the output file. The “copy” option copies the first audio stream from the first input file to the output file. The “default” and “title” options specify the default disposition and the title metadata for the video and audio streams, respectively. The “metadata:s:s:0” and “metadata:s:s:1” options specify the title metadata for the two subtitle streams.
The last line saves the output file in the “MUXED” directory with the same name as the original video file (“%%~na.mkv”).
The “pause” command at the end of the script prevents the command prompt window from closing automatically, allowing you to see any error messages.
Testing the Batch File
To test the batch file, save it with a “.bat” extension (e.g. “joinfiles.bat”) and place it in the same directory as the two input files. Then, double-click the batch file to run it.
The batch file should combine the two files and save the output file in the “MUXED” directory. If there are any errors, the command prompt window will display them.
Modifying the Batch File
If you have files with different extensions or if the files have different names, you can modify the batch file to accommodate those differences.
For example, if you have a video file with a “.mp4” extension and an audio file with a “.aac” extension, you can modify the batch file to look like this:
mkdir MUXED
FOR %%a IN ("*.mp4") DO ffmpeg -i "%%a" -i "%%~na.aac" ^
-map 1:v:0 c:v:0 copy -disposition:v:0 default -metadata:s:v:0 title="Example" ^
-map 0:a:0 -c:a:0 copy -disposition:a:0 default -metadata:s:a:0 title="DD 2.0" ^
"MUXED%%~na.mkv"
pause
This modified batch file uses “.mp4” as the video file extension and “.aac” as the audio file extension. The rest of the code is the same as before.
Conclusion
In this blog post, we showed you how to create a batch file that joins two files with similar names and the same extensions. We used FFmpeg to combine the files and saved the output file in a new directory. We also showed you how to modify the batch file to accommodate different file extensions or names.
Batch files are a powerful tool for automating repetitive tasks on your computer. With a little bit of scripting knowledge, you can create batch files to perform a wide variety of actions.
To join two files that have similar names and the same extension using a batch file, you can use a FOR loop to iterate through the files and use string manipulation to generate the names of the input files.
Here is an example of how you can modify your script to do this:
mkdir MUXED
FOR %%a IN ("*.mkv") DO (
set filename=%%~na
set encoded_filename=!filename!-Encoded
ffmpeg -i "!filename!.mkv" -i "!encoded_filename!.mkv" ^
-map 1:v:0 c:v:0 copy -disposition:v:0 default -metadata:s:v:0 title="Example" ^
-map 0:a:0 -c:a:0 copy -disposition:a:0 default -metadata:s:a:0 title="DD 2.0" ^
-map 0:a:1 -c:a:1 copy -metadata:s:s:0 title="Commentary" ^
-map 0:s:0 -c:s:0 copy -metadata:s:s:0 title="English (SRT)" ^
-map 0:s:1 -c:s:1 copy -metadata:s:s:1 title="English (VobSub)" ^
"MUXED\!filename!.mkv"
)
pause
This script uses the FOR loop to iterate through all the .mkv
files in the current directory. For each file, it sets the filename
variable to the name of the file without the extension, and the encoded_filename
variable to the filename
with -Encoded
appended to it. It then uses these variables to specify the input files for ffmpeg
.
The code you provided is written in batch script, which is a scripting language used in Windows operating systems to automate tasks and create simple programs. Batch script is executed by the command prompt (cmd.exe) on Windows and allows you to perform tasks such as running commands, setting variables, and looping through groups of files.
The script uses a FOR loop to iterate through the .mkv
files in the current directory, and within the loop it uses the set
command to assign values to variables and the !
operator to access the value of a variable. It also uses the ^
symbol to continue a command over multiple lines.
I hope this helps clarify the language used in the script. Let me know if you have any other questions.