Log in

View Full Version : mjpeg to mpeg4


mrcubehead
14th February 2006, 06:46
I wanted to recompress my Canon SD400 mjpeg video captures to mp4 for archiving, and after a lot of tinkering and hair-pulling, I wrote a little Ruby script to do it (easy to change to any other scripting language). I hope it'll be of use to somebody else. Uses avisynth, mplayer, faac, x264, mp4box; assumes executables are in path, and script is running in same directory as target.

--------------------

# make sure it's an avi file

target = ARGV[0].sub(/\.avi$/,'')

if target['.'] or not ARGV[0]['.avi'] then
puts 'Must be .avi file'
exit
end

# generate AVS script

avsScript = File.new("#{target}.avs", "w")
avsScript.write("DirectShowSource(\"#{target}.avi\", fps=15, audio=false)\n")
avsScript.write("ChangeFPS(30)\n")
avsScript.write("ConvertToYV12()\n")
avsScript.close

# run tools

`mplayer.exe -ao pcm:file="#{target}.wav" -vc dummy -vo null "#{target}.avi"`
`faac.exe "#{target}.wav" "#{target}.aac`
`x264.exe --bitrate 700 --analyse p8x8,b8x8,i4x4 --progress --no-psnr --output "#{target}.mp4" "#{target}.avs"`
`MP4Box.exe -add "#{target}.aac" -add "#{target}.mp4" "#{target}_mux.mp4"`

# clean up

`del "#{target}.wav"`
`del "#{target}.aac"`
`del "#{target}.avs"`
`del "#{target}.mp4"`
`copy "#{target}_mux.mp4" "#{target}.mp4"`
`del "#{target}_mux.mp4"`

fluor
21st April 2011, 14:07
thank you very much! I have added a script in batch that do pretty much the same.

You need the following files: Not hard to find.
faac.exe
mplayer.exe
x264.exe
DeGrainMedian.dll (for avisynth, as canon has very much noise)

Run as: canon_2_x265.bat input.avi
Can be added to right-click "send to"-menu. (http://www.howtogeek.com/howto/windows-vista/customize-the-windows-vista-send-to-menu/). Then you can right-click on several files and then send-to this script!

canon_2_x265.bat:

@echo off
if %1x==x goto :usage
set utilDir=%~dp0
set i=0
:loop

set output=%~n1_x264.mkv
echo -------------------
echo Compressing %1 to %output% ....
echo -------------------
echo DirectShowSource(%1, fps=30, audio=false) >"tmp.avs"
echo ConvertToYV12()>>"tmp.avs"
echo loadplugin("%utilDir%DeGrainMedian.dll")>>"tmp.avs"
echo DegrainMedian(limitY=4,limitUV=4,mode=1)>>"tmp.avs"
echo Temporalsoften(2,5,5,mode=2,scenechange=6)>>"tmp.avs"

echo Extracting WAV...
"%utilDir%mplayer.exe" -ao pcm:file="tmp.wav" -vc dummy -vo null %1 >nul
echo Packing AAC...
"%utilDir%faac.exe" "tmp.wav" -o "tmp.aac" >nul
echo Packing x264...
"%utilDir%x264.exe" --quiet --bitrate 2700 --analyse p8x8,b8x8,i4x4 --output "tmp.mp4" "tmp.avs"
echo Merging to MKV...
"%utilDir%mkvmerge.exe" -o "%~dp1%output%" "tmp.mp4" "tmp.aac" >nul

del "tmp.mp4"
del "tmp.wav"
del "tmp.aac"
del "tmp.avs"
set /a i=%i%+1
echo -------------------
echo %output% written to current dir.
echo -------------------

shift
if not "%~1"=="" goto loop

echo Finished. %i% files hopefully converted to x264.
pause

goto :eof

:usage
echo Fluors Canon_2_x264.bat
echo Converts input-files (MJPEG) from Canon-cameras to x264.
echo Tip: Add this to send-to menu in Windows. Then you can
echo right-click and compress several movies at once.
echo .
echo USAGE: Canon_2_x264.bat [filename to be converted]
pause
goto :eof

Blue_MiSfit
22nd April 2011, 03:08
I'd suggest a few things for your x264 cli:

1) Use CRF encoding. Maybe start with CRF20 and go up if you want more compression (less bitrate). This will give you much better results than 1 pass ABR, provided you can stomach some variability in your final bitrates.

2) Skip manual subpartition analysis. Set --tune film and the slowest --preset value you can tolerate.

Also, I'd suggest you look into using ffvideosource instead of relying on directshow to decode your mjpeg AVI files. They're likely natively YV12, and obviously directshow is returning something other than YV12 (since you explicitly convert to yv12 in your script). Finally, you might look into some other denoising filters. Degrainmedian is wicked fast and isn't too destructive, but there are tons of other options. TemporalSoften is pretty aggressive. Maybe try HQDN3D, or one of the mdegrain scripts. These will of course add more encode time.

Other than that, clever script :)

Derek

fluor
22nd April 2011, 10:56
thanks. i have now started experimenting with quality-settings. canon produces a lot of noise in dark scenes. i cannot find FFVideoSource in avisynth

smok3
22nd April 2011, 11:46
i cannot find FFVideoSource in avisynth

http://code.google.com/p/ffmpegsource/downloads/list

uciekamy
2nd October 2012, 20:19
Hi. Sorry for bumping the old topic but i was searching for any way to convert my Canon videos to mp4 (playable in standalones) and the solution posted here seem to be to hard for me - donwloaded the posted files but can't get it working. Is there any other easy way to convert that videos in good quality (that denoising seem to be needed)? I like VirtualDub but it can't open mjpeg files..
I'll be thankful for ideas.

Atak_Snajpera
2nd October 2012, 20:39
have you tried atleast guis in mpeg4 gui section?

smok3
3rd October 2012, 08:41
Hi. Sorry for bumping the old topic but i was searching for any way to convert my Canon videos to mp4 (playable in standalones) and the solution posted here seem to be to hard for me - donwloaded the posted files but can't get it working. Is there any other easy way to convert that videos in good quality (that denoising seem to be needed)? I like VirtualDub but it can't open mjpeg files..
I'll be thankful for ideas.

VirtualDub used to have built in mjpeg decoder (perhaps you need to enable that), but to answer the question; use ffmpeg with x264 and some AAC encoder, mux/save to mp4 or mkv (or as allready mentioned one of the gazillion guis).

uciekamy
3rd October 2012, 20:06
OK guys, thanks. I'll check it.