Avi h264 to mp4
From tiberious.org
Contents |
[edit]
Converting an h264 stream in an avi file to mp4
For windows only
[edit]
Requirements
Place all the files in the same directory
- mp4box: http://www.videohelp.com/tools/mp4box
- mkvtoolnix: http://www.bunkus.org/videotools/mkvtoolnix/ [for this i copied the following files into the file
- mkvmerge
- mkvextract
- tc2mp4: http://sidheog.org/Documents/tc2mp4_20070124.rar
- Password: doom9.org
[edit]
What happens
- convert the avi file to an mkv file
- extract the stream from the mkv file
- extract the timecode from the mkv file
- mux the extracted video from into an mp4 file
- use the mkv timecode and the mp4 file to create a timecoded mp4 file
- mux the timecoded mp4 file and the audio file
[edit]
Script
The following is a batch file which takes the first argument as an avi file with an h264 stream @ 20fps and an mp3 stream
Note: The fps and stream types are hard coded, change them if needed
- Create a file in a directory with the files from above called: avi2mp4.bat
- Edit the file and paste the following
- run avi2mp4 infile where infile is the location of the avi file to convert
- a file infile.mp4 should be created with the video and audio
@echo off rem avi2mp4.bat takes a avi with an h264 rem section and puts it into an mp4 file rem The command uses the following syntax: rem avi2mp4 path file :check if (%1)==() goto help if (%1)==(/?) goto help set bpath=%~dp0 set path=%path%;%bpath% set lfile="%~f1" set lpath=%~dp1 set lfileroot=%~n1 if not exist %lfile% goto nofile :merge rem ================================================= rem merge into mkv file first rem this is done so we can extract the rem contents with correct time codes later on rem for muxing into mp4 rem ================================================= set lmkvfile="%lpath%%lfileroot%.mkv" rem Check if file exists if exist %lmkvfile% del %lmkvfile% echo. echo Merging video and audio into mkv file: %lmkvfile% echo "mkvmerge" -o %lmkvfile% %lfile% echo. "mkvmerge" -o %lmkvfile% %lfile% :extract rem ================================================= rem extract from mkv file rem ================================================= set lvideofile="%lpath%%lfileroot%.h264" set laudiofile="%lpath%%lfileroot%.mp3" set ltimecodefile="%lpath%%lfileroot%.tc.txt" rem Check if files exist if exist %lvideofile% del %lvideofile% if exist %laudiofile% del %laudiofile% if exist %ltimecodefile% del %ltimecodefile% echo. echo Extracting video and audio from mkv file echo "mkvextract" tracks %lmkvfile% 1:%lvideofile% 2:%laudiofile% echo. "mkvextract" tracks %lmkvfile% 1:%lvideofile% 2:%laudiofile% echo. echo Extracting timecode from mkv file echo "mkvextract" timecodes_v2 %lmkvfile% 1:%ltimecodefile% echo. "mkvextract" timecodes_v2 %lmkvfile% 1:%ltimecodefile% :muxtmpmp4 rem ================================================= rem mux into temp mp4 file rem ================================================= set ltmpmp4file="%lpath%%lfileroot%.tmp.mp4" rem check if file exists if exist %ltmpmp4file% del %ltmpmp4file% echo. echo Mux video into a temporary file echo "MP4Box" -add %lvideofile% -fps 20 %ltmpmp4file% echo. "MP4Box" -add %lvideofile% -fps 20 %ltmpmp4file% :tc2mp4 rem ================================================= rem use the timecode from mkv file to fix the mp4 file rem ================================================= set ltcmp4file="%lpath%%lfileroot%.tc.mp4" rem check if file exists if exist %ltcmp4file% del %ltcmp4file% echo. echo Using the timecode from the mkv file to fix the mp4 file echo This may take some time echo "tc2mp4" -i %ltmpmp4file% -o %ltcmp4file% -t %ltimecodefile% echo. "tc2mp4" -i %ltmpmp4file% -o %ltcmp4file% -t %ltimecodefile% :muxfinalmp4 rem ================================================= rem use the output from the timecode fixed mp4 to rem mux into a new mp4 file rem ================================================= set lmp4file="%lpath%%lfileroot%.mp4" rem check if file exists if exist %lmp4file% del %lmp4file% rem check if an audio file exists if not exist %laudiofile% ( echo. echo Muxing the timecode fixed mp4 file echo "MP4Box" -add %ltcmp4file% %lmp4file% echo. "MP4Box" -add %ltcmp4file% %lmp4file% goto del ) echo. echo Muxing the timecode fixed mp4 file with audio echo "MP4Box" -add %ltcmp4file% -add %laudiofile% %lmp4file% echo. "MP4Box" -add %ltcmp4file% -add %laudiofile% %lmp4file% :del rem ================================================= rem delete temporary files rem ================================================= echo Deleting temporary files... if exist %lvideofile% del %lvideofile% if exist %laudiofile% del %laudiofile% if exist %ltimecodefile% del %ltimecodefile% if exist %lmkvfile% del %lmkvfile% if exist "%lpath%%lfileroot%.tmp_track1.info" del "%lpath%%lfileroot%.tmp_track1.info" if exist %ltmpmp4file% del %ltmpmp4file% if exist %ltcmp4file% del %ltcmp4file% set lvideofile="" set laudiofile="" set ltimecodefile="" set lmkvfile="" set ltmpmp4file="" set ltcmp4file="" echo Complete, final file: %lmp4file% set lmp4file="" goto end :help echo avi2mp4.bat takes a avi with an h264 echo section and puts it into an mp4 file echo The command uses the following syntax: echo avi2mp4 path file goto end :nofile echo File: %lfile% does not exist. :end set lpath="" set lfile="" set lfileroot="" set bpath="" @echo on
