Here's how you can automate the process of extracting video and audio file names into text files and then merging them using a batch script in
Windows:
Step 1: Script Overview
This script will:
- Generate a list of video file names (videos.txt).
- Generate a list of audio file names (audios.txt).
- Match video files with corresponding audio files by name.
- Merge the audio and video files using FFmpeg.
Step 2: Create the Script
- Open Notepad and paste the following script:
@echo off
REM Create lists of video and audio files
dir /b *.mp4 > videos.txt
dir /b *.mp3 > audios.txt
REM Read video and audio file names from the lists and merge them
for /f "tokens=*" %%V in (videos.txt) do (
set "video=%%V"
set "audio=%%~nV.mp3"
if exist "%%~nV.mp3" (
echo Merging %%V and %%~nV.mp3...
ffmpeg -i "%%V" -i "%%~nV.mp3" -c:v copy -c:a aac "output_%%~nV.mp4"
) else (
echo Audio file for %%V not found!
)
)
REM Cleanup
del videos.txt
del audios.txt
echo All done!
pause
- Save the file as merge.bat in the same folder as your video and audio files.
Step 3: Run the Script
- Double-click the merge.bat file.
- The script will:
- List all .mp4 files into videos.txt.
- List all .mp3 files into audios.txt.
- Match each video file with its corresponding audio file (based on the same name without extensions).
- Merge them into new files named output_<video_name>.mp4.
How It Works
- The dir /b command lists all .mp4 and .mp3 files in the directory.
- The script matches video files with audio files using the same base name (%%~nV).
- If a matching audio file exists, FFmpeg merges the video and audio.
- If no matching audio file is found, the script logs an error message.
Example
Folder Contents:
- video1.mp4
- video2.mp4
- video1.mp3
- video3.mp3
Output:
- output_video1.mp4 (merged successfully)
- output_video2.mp4 (error: audio not found)
- output_video3.mp4 (error: video not found)
another method
To merge audio and video files using FFmpeg in Windows, you can follow these steps:
1. Install FFmpeg
- Download FFmpeg from the official website: https://ffmpeg.org/.
- Extract the downloaded files and add the bin folder to your system's PATH environment variable so you can use FFmpeg from the command line.
2. Prepare Your Files
- Place your video file (e.g., video.mp4) and audio files (e.g., audio1.mp3, audio2.mp3, etc.) in the same folder for convenience.
3. Concatenate Audio Clips
If you have multiple audio files to combine, create a text file (audio_list.txt) with the following format:
file 'audio1.mp3'
file 'audio2.mp3'
file 'audio3.mp3'
- Open Notepad, write the list of audio files in the above format, and save it as audio_list.txt in the same folder as your files.
- Run the following command in Command Prompt (cmd) to merge the audio clips:
ffmpeg -f concat -safe 0 -i audio_list.txt -c copy merged_audio.mp3
This will create a single audio file called merged_audio.mp3.
4. Merge Video and Audio
Now, merge the video file (video.mp4) with the combined audio file (merged_audio.mp3):
- Open Command Prompt and run:
ffmpeg -i video.mp4 -i merged_audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4
- video.mp4: Your video file.
- merged_audio.mp3: The combined audio file.
- output.mp4: The final output file with the merged audio and video.
5. Sync Audio and Video (Optional)
- If the audio is shorter than the video and you need to loop it:
ffmpeg -stream_loop -1 -i merged_audio.mp3 -i video.mp4 -shortest -c:v copy -c:a aac output.mp4
- If the audio is longer than the video and you want to trim it:
cmd
Copy code
ffmpeg -i merged_audio.mp3 -i video.mp4 -filter_complex "[0:a]apad" -shortest -c:v copy output.mp4
6. Automate for Multiple Files
If you have many video and audio files, use a batch script:
- Create a new text file in Notepad and paste the following:
@echo off
for %%v in (*.mp4) do (
set "video=%%v"
set "audio=%%~nv.mp3"
ffmpeg -i "%%v" -i "%%~nv.mp3" -c:v copy -c:a aac "output_%%~nv.mp4"
)
pause
- Save it as merge.bat in the folder with your video and audio files.
- Double-click the batch file to run it. It will process all .mp4 videos with corresponding .mp3 audio files.