Results 1 to 10 of 17

Thread: ffmpeg Script for batch convert video files into mp4 for iDevice really fast!

Threaded View

  1. #1
    Join Date
    Sep 2008
    Beans
    37

    ffmpeg Script for batch convert video files into mp4 for iDevice really fast!

    =========== UPDATED Aug 16 2013 ===========

    Hello
    I'm a ubuntu user and a Mac / iDevice / iTunes user.

    Because of the restriction of iTunes, you can't play divx or mkv files in iPhone / iPod / or iPad. even Xboxs / PS3

    so you need to convert files. and a lots of files.
    most convertor sucks. they are slow!
    actually you can make it faster.

    you know: video files have
    container: like mp4 mkv avi (change container is superfast)
    video: like h.264 divx, (change video format is superslow)
    audio: like mp3 aac ac3 5.1 dts (change audio format is fast)

    Now lots of mkvs now encoded in H.264 that means you don't need to re-encode the video part. that save a lots of time!

    unlike other tools I want something can decide what to convert what to copy automaticlly.

    So I start to learn shell script and here is my first shell script



    How to use:
    it come's with 2 files
    ffbc.sh
    ffbc_atv.sh

    ffbc.sh is for all devices but it will convert AC3 5.1 audio into aac 2 channel.

    ffbc_atv.sh is for Apple TV it won't convert AC3 5.1 audio but copy it into new file because APPLE TV can play/passthrought AC3 5.1 audio

    0. make sure you have ffmpeg 2.x with libfaac and x264 installed. (there are 2 ffmpegs, this script works for the ffmpeg from ffmpeg.org not the avconv)
    1. put all video files into a folder (File name MUST NOT have Spaces)
    2. make sure you have rights for write into the folder
    3. cd the folder contain the scripts.
    4. type the command

    like:

    Code:
    sh ffbc.sh mp4 /home/username/video
    this will start to convert all video files in /home/username/video into mp4 format which iDevices can play.

    if you want to covert into mkv for further process try

    Code:
    sh ffbc.sh mkv /home/username/video
    this will start to convert all video files in /home/username/video into mkv with H.264 video and aac audio.

    5. find new file in output folder




    Script: ffbc.sh
    Code:
    #! /bin/bash
    
    # Batch Convert Script by Beterhans based on FFMPEG
    # The Purpose of this Script is to convert any video file to mp4 format
    # which most hardware player like iPhone/iPod PS3 Xbox360 could play.
    # you can even convert to mkv with h.264 + aac for further process
    # this script only convert necessary tracks if the video is already
    # in H.264 format it won't convert it save your great time! and quality.
    
    # if you want to make multi-track audio/video/sub mp4 you should convert
    # to mkv and then use a MAC OS X app called "mp4tools"
    # to mix them down into m4v
    
    # Put all video files need to be converted in a folder!
    # the name of files must not have " " Space!
    # Rename the File if contain space 
    
    
    # Variable used
    # indir
    # ffversion the version if your ffmpeg
    # outmode is out put for mkv or mp4
    # outformat is used by ffmpeg
    # vcodec acodec for ffmpeg
    
    # working mode
    outmode=$1
    
    # Target dir
    indir=$2
    cd "$indir"
    if mkdir -p output
    	then
    	 echo "\nUsing $indir/output as Output Folder"
    	else
    	 echo "\n\nErr: Check you have the rights to write in $indir ?"
    	 exit
    fi
    
    
    # check output mode
    if [ $outmode = "mp4" ] || [ $outmode = "mkv" ]
    	then 
    	echo "\nWORKING MODE $outmode"
    	else
    	echo "$outmode is NOT a Correct target format\nYou need to set an output format! like "ffbc mp4 xxxx" or ffbc mkv xxxx"
    	exit
    fi
    
    # set format
    if [ $outmode=mp4 ]
    	then
    	 outformat=mp4
    	else
    	 outformat=matroska
    fi
    
    # Check FFMPEG Installation
    if ffmpeg -formats > /dev/null 2>&1 
    	then
    	 ffversion=`ffmpeg -version 2> /dev/null | grep ffmpeg | sed -n 's/ffmpeg\s//p'`
    	 echo "Your ffmpeg verson is $ffversion"
    	else
    	 echo "\nERROR:\nYou need ffmpeg installed with x264 and an aac encoder"
    	 exit
    fi
    
    if ffmpeg -formats 2> /dev/null | grep "E mp4" > /dev/null
    	then
    	 echo "Check mp4 container format ... OK"
    	else
    	 echo "Check mp4 container format ... NOK"
    	 exit
    fi
    
    if ffmpeg -formats 2> /dev/null | grep "E matroska" > /dev/null
            then
             echo "Check mkv container format ... OK"
            else
             echo "Check mkv container format ... NOK"
             exit
    fi
    
    if ffmpeg -codecs 2> /dev/null | grep "libfaac" > /dev/null
            then
             echo "Check fAAC Audio Encoder ... OK"
            else
             echo "Check fAAC Audio Encoder ... NOK"
             exit
    fi
    
    if ffmpeg -codecs 2> /dev/null | grep "libx264" > /dev/null
            then
             echo "Check x264 the free H.264 Video Encoder ... OK"
            else
             echo "Check x264 the free H.264 Video Encoder ... NOK"
             exit
    fi
    
    echo "Your FFMpeg is OK\nEntering File Processing\n\n"
    
    ################################################################
    
    for filelist in `ls`
    do
    	if ffmpeg -i $filelist 2>&1 | grep 'Invalid data found'		#check if it's video file
    	   then
    	   echo "\n\nERROR\nFile $filelist is NOT A VIDEO FILE can be converted!"
    	   exit	   
    	
    	fi
    
    	if ffmpeg -i $filelist 2>&1 | grep Video: | grep h264		#check video codec
    	   then
    	    vcodec=copy
    	   else
    	    vcodec=libx264
    	fi
    
    	if ffmpeg -i $filelist 2>&1 | grep Video: | grep "High 10"	#10 bit H.264 can't be played by Hardware.
    	   then
    	    vcodec=libx264
    	fi
    
    	if ffmpeg -i $filelist 2>&1 | grep Audio: | grep aac		#check audio codec
    	   then
    	    acodec=copy
    	   else
    	    acodec=libfaac
    	fi
    
    	echo "\n\nUseing video codec: $vcodec audio codec: $acodec and Container format $outformat for\nFile: $filelist\n\n\n\nStarting Converting\n" 
    
    # using ffmpeg for real converting
    	echo "ffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 128k -ac 2 -absf aac_adtstoasc -async 1 -vcodec $vcodec -vsync 0 -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 ./output/$filelist.$outmode\n\n"
    	ffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 128k -ac 2 -absf aac_adtstoasc -async 1 -vcodec $vcodec -vsync 0 -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 ./output/$filelist.$outmode
    
    	
    done
    	echo ALL Processed!
    
    
    
    ###################
    echo "DONE"
    exit




    Script: ffbc_atv.sh
    Code:
    #! /bin/bash
    
    # Batch Convert Script by Beterhans based on FFMPEG
    # The Purpose of this Script is to convert any video file to mp4 format
    # which most hardware player like iPhone/iPod PS3 Xbox360 could play.
    # !Note! this script won't convert AC3 Stream. 
    # becase Apple TV are capable passthrough AC3 stream
    # you can even convert to mkv with h.264 + aac for further process
    # this script only convert necessary tracks if the video is already
    # in H.264 format it won't convert it save your great time! and quality.
    
    # if you want to make multi-track audio/video/sub mp4 you should convert
    # to mkv and then use a MAC OS X app called "mp4tools"
    # to mix them down into m4v
    
    # Put all video files need to be converted in a folder!
    # the name of files must not have " " Space!
    # Rename the File if contain space 
    
    
    # Variable used
    # indir
    # ffversion the version if your ffmpeg
    # outmode is out put for mkv or mp4
    # outformat is used by ffmpeg
    # vcodec acodec for ffmpeg
    
    # working mode
    outmode=$1
    
    # Target dir
    indir=$2
    cd "$indir"
    if mkdir -p output
    	then
    	 echo "\nUsing $indir/output as Output Folder"
    	else
    	 echo "\n\nErr: Check you have the rights to write in $indir ?"
    	 exit
    fi
    
    
    # check output mode
    if [ $outmode = "mp4" ] || [ $outmode = "mkv" ]
    	then 
    	echo "\nWORKING MODE $outmode"
    	else
    	echo "$outmode is NOT a Correct target format\nYou need to set an output format! like "ffbc mp4 xxxx" or ffbc mkv xxxx"
    	exit
    fi
    
    # set format
    if [ $outmode=mp4 ]
    	then
    	 outformat=mp4
    	else
    	 outformat=matroska
    fi
    
    # Check FFMPEG Installation
    if ffmpeg -formats > /dev/null 2>&1 
    	then
    	 ffversion=`ffmpeg -version 2> /dev/null | grep ffmpeg | sed -n 's/ffmpeg\s//p'`
    	 echo "Your ffmpeg verson is $ffversion"
    	else
    	 echo "\nERROR:\nYou need ffmpeg installed with x264 and an aac encoder"
    	 exit
    fi
    
    if ffmpeg -formats 2> /dev/null | grep "E mp4" > /dev/null
    	then
    	 echo "Check mp4 container format ... OK"
    	else
    	 echo "Check mp4 container format ... NOK"
    	 exit
    fi
    
    if ffmpeg -formats 2> /dev/null | grep "E matroska" > /dev/null
            then
             echo "Check mkv container format ... OK"
            else
             echo "Check mkv container format ... NOK"
             exit
    fi
    
    if ffmpeg -codecs 2> /dev/null | grep "libfaac" > /dev/null
            then
             echo "Check fAAC Audio Encoder ... OK"
            else
             echo "Check fAAC Audio Encoder ... NOK"
             exit
    fi
    
    if ffmpeg -codecs 2> /dev/null | grep "libx264" > /dev/null
            then
             echo "Check x264 the free H.264 Video Encoder ... OK"
            else
             echo "Check x264 the free H.264 Video Encoder ... NOK"
             exit
    fi
    
    echo "Your FFMpeg is OK\nEntering File Processing\n\n"
    
    ################################################################
    
    for filelist in `ls`
    do
    	if ffmpeg -i $filelist 2>&1 | grep 'Invalid data found'		#check if it's video file
    	   then
    	   echo "\n\nERROR\nFile $filelist is NOT A VIDEO FILE can be converted!"
    	   exit	   
    	
    	fi
    
    	if ffmpeg -i $filelist 2>&1 | grep Video: | grep h264		#check video codec
    	   then
    	    vcodec=copy
    	   else
    	    vcodec=libx264
    	fi
    
    	if ffmpeg -i $filelist 2>&1 | grep Video: | grep "High 10"	#10 bit H.264 can't be played by Hardware.
    	   then
    	    vcodec=libx264
    	fi
    
    	if ffmpeg -i $filelist 2>&1 | grep Audio: | grep aac		#check audio codec
    	   then
    	    acodec=copy
    	elif ffmpeg -i $filelist 2>&1 | grep Audio: | grep 5.1		#check audio codec
    	   then
    	    acodec=copy
    	   else
    	    acodec=libfaac
    	fi
    
    	echo "\n\nUseing video codec: $vcodec audio codec: $acodec and Container format $outformat for\nFile: $filelist\n\n\n\nStarting Converting\n" 
    
    # using ffmpeg for real converting
            echo "Command:\nffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 256k -ac 2 -absf aac_adtstoasc -vcodec $vcodec -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 ./output/$filelist.$outmode\n\n"
            ffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 256k -ac 2 -absf aac_adtstoasc -vcodec $vcodec -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 ./output/$filelist.$outmode
    	
    done
    	echo ALL Processed!
    
    
    
    ###################
    echo "DONE"
    exit
    If you find the code won't work for your ffmepg
    try this new one. it works with newer ffmpeg
    http://www.sendspace.com/file/9n9wwo
    Last edited by beterhans; August 16th, 2013 at 02:14 PM. Reason: update

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •