Saturday 24 August 2013

make videos by linux + PowerDirector

Recently I need to do a video project for my daughter's birthday. the project involves two chapters: the first chapter is to slide her photos with 0.2 seconds interval, each photos has approximately the same head size and eye position is close. The second chapter is to combine all of the interesting videos of her.


The first one is going to be massive if one uses gimp or photoshop to do that. therefore, I decided to use scripts to handle these problem. After digging, I found opencv in python and matlab has the function of finding faces from files. So I decided to use opencv as a start. I found a code (updated from http://stackoverflow.com/questions/13211745/detect-face-then-autocrop-pictures) I did further change so that each output file has put the face right in the middle with same size (translations and scaling)

here is the working scripts :


'''
Sources:
http://opencv.willowgarage.com/documentation/python/cookbook.html
http://www.lucaamore.com/?p=638
'''

#Python 2.7.2
#Opencv 2.4.2
#PIL 1.1.7

import cv #Opencv
import Image #Image from PIL
import glob
import os


# x correction
adj=0.05
# the width of output image
picx = 1024.1
# the height of output image
picy = 768.1
# the x ratio of picture in the whole output image
xpra=0.4
# the x ratio where picture starts
xra = (1-xpra-adj)/2
# the y ratio where picture starts
yra = (picy-xpra*picx)/picy/2



def DetectFace(image, faceCascade, returnImage=False):
    # This function takes a grey scale cv image and finds
    # the patterns defined in the haarcascade function
    # modified from: http://www.lucaamore.com/?p=638

    #variables    
    min_size = (200,200)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0



    
    # Equalize the histogram
    cv.EqualizeHist(image, image)

    # Detect the faces
    faces = cv.HaarDetectObjects(
            image, faceCascade, cv.CreateMemStorage(0),
            haar_scale, min_neighbors, haar_flags, min_size
        )

    # If faces are found
    if faces and returnImage:
        for ((x, y, w, h), n) in faces:
            # Convert bounding box to two CvPoints
            pt1 = (int(x), int(y))
            pt2 = (int(x + w), int(y + h))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)
    if returnImage:
        return image
    else:
        return faces

def pil2cvGrey(pil_im):
    # Convert a PIL image to a greyscale cv image
    # from: http://pythonpath.wordpress.com/2012/05/08/pil-to-opencv-image/
    pil_im = pil_im.convert('L')
    cv_im = cv.CreateImageHeader(pil_im.size, cv.IPL_DEPTH_8U, 1)
    cv.SetData(cv_im, pil_im.tostring(), pil_im.size[0]  )
    return cv_im

def cv2pil(cv_im):
    # Convert the cv image to a PIL image
    return Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())

def imgCrop(image, cropBox, boxScale=1):
    # Crop a PIL image with the provided box [x(left), y(upper),
    # w(width), h(height)]

    # Calculate scale factors
    xDelta=max(cropBox[2]*(boxScale-1),0)
    yDelta=max(cropBox[3]*(boxScale-1),0)

    # Convert cv box to PIL box [left, upper, right, lower]
    PIL_box=[cropBox[0]-xDelta,
             cropBox[1]-yDelta,
             cropBox[0]+cropBox[2]+xDelta,
             cropBox[1]+cropBox[3]+yDelta]

    return image.crop(PIL_box)

def faceCrop(imagePattern,boxScale=1):
    # Select one of the haarcascade files:
    #   haarcascade_frontalface_alt.xml  <-- Best one?
    #   haarcascade_frontalface_alt2.xml
    #   haarcascade_frontalface_alt_tree.xml
    #   haarcascade_frontalface_default.xml
    #   haarcascade_profileface.xml
    faceCascade = cv.Load(
        '/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml')
    #  faceCascade = cv.Load('haarcascade_frontalface_alt.xml')

    imgList=glob.glob(imagePattern)
    if len(imgList)<=0:
        print 'No Images Found'
        return

    for img in imgList:
        pil_im=Image.open(img)
        cv_im=pil2cvGrey(pil_im)
        faces=DetectFace(cv_im,faceCascade)
        if faces:
            n=1
            for face in faces:
               #  scale factor new/old
               scf=(picx*xpra)/face[0][3]
               #  new =old*scf
               rsx=int(pil_im.size[0]*scf)
               rsy=int(pil_im.size[1]*scf)
               resizedImage=pil_im.resize(
                   (rsx,rsy)
                   ,Image.ANTIALIAS)
               croppedImage=imgCrop(
                   resizedImage,
                   (int(face[0][0]*scf-xra*picx),int(face[0][1]*scf-yra*picy),int(picx),int(picy)),
#                   (int(face[0][0]*scf),int(face[0][1]*scf),int(picx),int(picy)),
#                   (int(face[0][0]*scf),int(face[0][1]*scf),int(face[0][2]*scf),int(face[0][3]*scf)),
                   boxScale=boxScale)  
               croppedImage2=imgCrop(pil_im, face[0],boxScale=boxScale)
               fname,ext=os.path.splitext(img)
               #resizedImage.save(fname+'_resized'+str(n)+ext)
               croppedImage.save(fname+'_r_c'+str(n)+ext)               
               croppedImage2.save(fname+'_cropori'+str(n)+ext)
               n+=1
        else:
            print 'No faces found:', img



            
def test(imageFilePath):
    pil_im=Image.open(imageFilePath)
    cv_im=pil2cvGrey(pil_im)
    # Select one of the haarcascade files:
    #   haarcascade_frontalface_alt.xml  <-- Best one?
    #   haarcascade_frontalface_alt2.xml
    #   haarcascade_frontalface_alt_tree.xml
    #   haarcascade_frontalface_default.xml
    #   haarcascade_profileface.xml
    faceCascade = cv.Load('haarcascade_frontalface_alt.xml')
    face_im=DetectFace(cv_im,faceCascade, returnImage=True)
    img=cv2pil(face_im)
    img.show()
    img.save('test.png')


# Test the algorithm on an image
#test('testPics/faces.jpg')

# Crop all jpegs in a folder. Note: the code uses glob which
# follows unix shell rules.
# Use the boxScale to scale the cropping area. 1=opencv box,
#    2=2x the width and height
#faceCrop('/home/debianchenming/Downloads/1/*.JPG',boxScale=1)
faceCrop('/media/D0CC0864CC0846E6/Portrait/secondtime/12-12-09/*.JPG',boxScale=1)
Notes:
1. be careful of the last absolut path, one has to change the path before running it.
2. to use the code, one put all of the photos as well as this code file (e.g., python.py) in one folder, then run:
    python python.py
 new files will be generated within this folder.
3. the result has shown that is code may fail for some of the photos around (3 out of 10)
4. more functionality should be added into the code, a, rotate the picture based on the lines between the eyes     b, make it work for the portrait photos as well. 

at least this code is able to save huge amount of time for photo editing.

once all of the photos are ready, one can put them in one folder and run 
$  mencoder -speed 0.4 "mf://*.JPG" -o movie_drilldata.04.avi -ovc lavc -lavcopts vcodec=mjpeg

here increasing parameter -speed 0.4 shortens the interval between neighbouring photos. with this done, the first chapter is finished. 

For the second chapter, one has to find out all the interesting scenes from video database. this work can be tedious and time consuming. once one interesting spot is found, copy the file into one folder and record the starting time and end time (or duration). with all this information done, one can generate each scene by bash commands. for me I have the following scripts:
#-------start of the file----------------------

#!/bin/bash
(\
width=720;height=480; \
ffmpeg -i                                           born.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:04 -t 00:00:07 -vf "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"                                             born_mpeg4.avi ;  \
ffmpeg -i                                        2setoff.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:06 -t 00:00:08                                          2setoff_mpeg4.avi ;  \
ffmpeg -i                                       6grandma.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:39 -t 00:00:08                                         6grandma_mpeg4.avi ;  \
ffmpeg -i                                          9swim.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:12 -t 00:00:05                                            9swim_mpeg4.avi ;  \ 
ffmpeg -i                                            cry.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:18 -t 00:00:09                                              cry_mpeg4.avi ;  \
ffmpeg -i                                    firstshower.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:03:23 -t 00:00:10                                      firstshower_mpeg4.avi ;  \
ffmpeg -i                  VID_20120902_173155_firstmilk.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:38 -t 00:00:06                    VID_20120902_173155_firstmilk_mpeg4.avi ;  \
ffmpeg -i                    06092012172_smallinthechair.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:00 -t 00:00:06                      06092012172_smallinthechair_mpeg4.avi ;  \
ffmpeg -i                VID_20120915_172404daddyrocking.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:01 -t 00:00:07                  VID_20120915_172404daddyrocking_mpeg4.avi ;  \
ffmpeg -i                  VID_20120916_223024taichi_cry.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:12 -t 00:00:08                    VID_20120916_223024taichi_cry_mpeg4.avi ;  \
ffmpeg -i             VID_20120929_200010_first_reacting.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:15 -t 00:00:08               VID_20120929_200010_first_reacting_mpeg4.avi ;  \
ffmpeg -i   VID_20121117_065733_smile_in_the_morning.mp4.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:50 -t 00:00:15     VID_20121117_065733_smile_in_the_morning.mp4_mpeg4.avi ;  \
ffmpeg -i                      VID_20121231_175216_savia.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:04 -t 00:00:06                        VID_20121231_175216_savia_mpeg4.avi ;  \
ffmpeg -i                 VID_20130103_123103eatbymyself.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:08 -t 00:00:07                   VID_20130103_123103eatbymyself_mpeg4.avi ;  \
ffmpeg -i                  VID_20130106_174149drumplayer.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:12 -t 00:00:18                    VID_20130106_174149drumplayer_mpeg4.avi ;  \
ffmpeg -i                 VID_20130128_100949lovemumsong.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:07 -t 00:00:08               VID_20130128_100949lovemumsong.mp4_mpeg4.avi ;  \
ffmpeg -i         VID_20130128_181959thinkduringfeeding2.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:02:10 -t 00:00:07           VID_20130128_181959thinkduringfeeding2_mpeg4.avi ;  \
ffmpeg -i       VID_20130129_183756thinkingduringfeeding.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:07 -t 00:00:08         VID_20130129_183756thinkingduringfeeding_mpeg4.avi ;  \
ffmpeg -i             VID_20130201_103404eat_during_sing.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:02:58 -t 00:00:10               VID_20130201_103404eat_during_sing_mpeg4.avi ;  \
ffmpeg -i                 VID_20130205_091441eatbymyself.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:00 -t 00:00:10                   VID_20130205_091441eatbymyself_mpeg4.avi ;  \
ffmpeg -i                      VID_20130214_085015scream.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:10 -t 00:00:08                        VID_20130214_085015scream_mpeg4.avi ;  \
ffmpeg -i               VID_20130214_135818laughatrattle.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:00 -t 00:00:13                 VID_20130214_135818laughatrattle_mpeg4.avi ;  \
ffmpeg -i                 VID_20130214_142152pickingnose.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:02:54 -t 00:00:07                   VID_20130214_142152pickingnose_mpeg4.avi ;  \
ffmpeg -i                        VID_20130320_095010shot.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:01:54 -t 00:00:10  -vf "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"                         VID_20130320_095010shot1_mpeg4.avi ;  \
ffmpeg -i                        VID_20130320_095010shot.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:01:23 -t 00:00:10 -vf "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"                        VID_20130320_095010shot2_mpeg4.avi ;  \
ffmpeg -i                    VID_20130420_174230painting.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:24 -t 00:00:11                      VID_20130420_174230painting_mpeg4.avi ;  \
ffmpeg -i                         VID_20130421_152130pat.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:10 -t 00:00:12                           VID_20130421_152130pat_mpeg4.avi ;  \
ffmpeg -i                     VID_20130502_121644eatfoot.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:00 -t 00:00:08                       VID_20130502_121644eatfoot_mpeg4.avi ;  \
ffmpeg -i                VID_20130503_131816imitatecough.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:00 -t 00:00:13                  VID_20130503_131816imitatecough_mpeg4.avi ;  \
ffmpeg -i                       VID_20130503_141256music.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:01:37 -t 00:00:07                         VID_20130503_141256music_mpeg4.avi ;  \
ffmpeg -i                      VID_20130506_111851cheese.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:09:16 -t 00:00:27                        VID_20130506_111851cheese_mpeg4.avi ;  \
ffmpeg -i                    VID_20130507_155121nothappy.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:08:29 -t 00:00:13                      VID_20130507_155121nothappy_mpeg4.avi ;  \
ffmpeg -i                      VID_20130112_095611libray.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:35 -t 00:00:05                        VID_20130112_095611libray_mpeg4.avi ;  \
ffmpeg -i                 VID_20130124_071841hideandseek.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:03 -t 00:00:07                   VID_20130124_071841hideandseek_mpeg4.avi ;  \
ffmpeg -i                 VID_20130205_091441eatbymyself.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:19 -t 00:00:03                   VID_20130205_091441eatbymyself_mpeg4.avi ;  \
ffmpeg -i                                    firstshower.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:08:29 -t 00:00:05                                      firstshower_mpeg4.avi ;  \
ffmpeg -i                   VID_20120903_102308_sunshine.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:09 -t 00:00:08                     VID_20120903_102308_sunshine_mpeg4.avi ;  \
ffmpeg -i                    VID_20120903_204930_withmom.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:00 -t 00:00:05 -vf "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"                     VID_20120903_204930_withmom_mpeg4.avi ;  \
ffmpeg -i                       VID_20120902_201000_dage.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:01 -t 00:00:06                         VID_20120902_201000_dage_mpeg4.avi ;  \
ffmpeg -i                       VID_20120830_190557dream.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:01:24 -t 00:00:06                         VID_20120830_190557dream_mpeg4.avi ;  \
ffmpeg -i                        VID_20120830_080226wrap.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:01 -t 00:00:05                          VID_20120830_080226wrap_mpeg4.avi ;  \
ffmpeg -i                 VID_20130223_153109water_melon.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:11 -t 00:00:04                   VID_20130223_153109water_melon_mpeg4.avi ;  \
ffmpeg -i                    VID_20130221_103317_reading.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:03 -t 00:00:07                      VID_20130221_103317_reading_mpeg4.avi ;  \
ffmpeg -i             VID_20130220_143129_listen_to_sing.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:18 -t 00:00:04               VID_20130220_143129_listen_to_sing_mpeg4.avi ;  \
ffmpeg -i               VID_20130215_133846_sit_on_chair.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:02 -t 00:00:08                 VID_20130215_133846_sit_on_chair_mpeg4.avi ;  \
ffmpeg -i             VID_20130207_142926dring_by_myself.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:04 -t 00:00:04               VID_20130207_142926dring_by_myself_mpeg4.avi ;  \
ffmpeg -i               VID_20130206_115009_happy_stroke.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:01 -t 00:00:07                 VID_20130206_115009_happy_stroke_mpeg4.avi ;  \
ffmpeg -i                       VID_20130129_111146_grip.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:29 -t 00:00:06                         VID_20130129_111146_grip_mpeg4.avi ;  \
ffmpeg -i                   VID_20130225_121326_eat_pear.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:01:51 -t 00:00:05                     VID_20130225_121326_eat_pear_mpeg4.avi ;  \
ffmpeg -i              VID_20130227_174230smash_pumpking.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:10 -t 00:00:05                VID_20130227_174230smash_pumpking_mpeg4.avi ;  \
ffmpeg -i                VID_20130307_113557_kiss_myself.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:22 -t 00:00:06                  VID_20130307_113557_kiss_myself_mpeg4.avi ;  \
ffmpeg -i              VID_20130314_113615scream_intrain.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:18 -t 00:00:06                VID_20130314_113615scream_intrain_mpeg4.avi ;  \
ffmpeg -i                                       1stretch.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:11 -t 00:00:16                                         1stretch_mpeg4.avi ;  \
ffmpeg -i                                     3happytime.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:00 -t 00:00:07 -s 720:480                                      3happytime_mpeg4.avi ;  \
ffmpeg -i                                        7splash.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:09 -t 00:00:06                                          7splash_mpeg4.avi ;  \
ffmpeg -i                                     10crawling.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:01:03 -t 00:00:06                                       10crawling_mpeg4.avi ;  \
ffmpeg -i                                      11library.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:13 -t 00:00:07                                        11library_mpeg4.avi ;  \
ffmpeg -i                    VID_20130503_102148eatpaper.mp4   -vcodec mpeg4 -vb 20M -r 30 -acodec copy -ss 00:00:51 -t 00:00:05                      VID_20130503_102148eatpaper_mpeg4.avi    \
)

#----------------------end of file-----------------
comments
1. here we choose -vcodec mpeg4 -vb 20M -r 30  as video output. this may results in very large output file, comparing with -vcodec libx264. However, with the experience to the editing software, I found that the source video files should be less compressed (or more basic method for encoding), although the file may become huge, but the editing experience becomes more fluent (sometimes it is compulsory like coral video studio does not accept avi file with libx264 code, also in powerdirector, libx264 can cause the sudden freeze at the transitions period, which is not good at all).
2. the line -vf "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2" is to transpose the video file without losing the aspect ratios (by adding bars on left and right hand side of the video).  

also I posted the one that works for iphone mov files. 


#------------------------begin of file-----------------
(\
width=720;height=480; \
ffmpeg -i      IMG_0013.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:03:30 -t 00:00:05 -vf "scale=$width:$height"                                                                                                                                                                          IMG_0013_copy.avi;   \   
ffmpeg -i      IMG_0016.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:04 -t 00:00:06 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0016_copy.avi;   \
ffmpeg -i      IMG_0044.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:58 -t 00:00:14 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0044_copy.avi;   \
ffmpeg -i      IMG_0067.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:08 -t 00:00:05 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0067_copy.avi;   \  
ffmpeg -i      IMG_0082.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:08 -t 00:00:08 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0082_copy.avi;   \   
ffmpeg -i      IMG_0088.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:04 -t 00:00:09 -filter:v "scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0088_copy.avi;   \   
ffmpeg -i      IMG_0089.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:03 -t 00:00:08 -vf "scale=$width:$height"                                                                                                                                                                          IMG_0089_copy.avi;   \  
ffmpeg -i      IMG_0099.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:02:26 -t 00:00:05 -vf "scale=$width:$height"                                                                                                                                                                          IMG_0099_copy.avi;   \   
ffmpeg -i      IMG_0103.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:02 -t 00:00:05 -vf "scale=$width:$height"                                                                                                                                                                          IMG_0103_copy.avi;   \    
ffmpeg -i      IMG_0105.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:00 -t 00:00:05 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0105_copy.avi;   \
ffmpeg -i      IMG_0112.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:13 -t 00:00:05 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0112_copy.avi;   \  
ffmpeg -i      IMG_0116.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:01:02 -t 00:00:07 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0116_copy.avi;   \  
ffmpeg -i      IMG_0117.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:03:40 -t 00:00:06 -vf "scale=$width:$height"                                                                                                                                                                          IMG_0117_copy.avi;   \  
ffmpeg -i      IMG_0156.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:49 -t 00:00:10 -filter:v "vflip,hflip, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0156_copy.avi;   \
ffmpeg -i      IMG_0163.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:14 -t 00:00:06 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0163_copy.avi;   \  
ffmpeg -i      IMG_0173.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:06 -t 00:00:07 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0173_copy.avi;   \  
ffmpeg -i      IMG_0186.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:04 -t 00:00:09 -vf "scale=$width:$height"                                                                                                                                                                          IMG_0186_copy.avi;   \  
ffmpeg -i      IMG_0191.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:31 -t 00:00:06 -vf "scale=$width:$height"                                                                                                                                                                          IMG_0191_copy.avi;   \
ffmpeg -i      IMG_0236.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:09 -t 00:00:39 -vf "scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"         IMG_0236_copy.avi;   \
ffmpeg -i      IMG_0250.MOV   -vcodec mpeg4 -vb 40M -r 30 -acodec copy -ss 00:00:00 -t 00:00:05 -filter:v "transpose=1, scale=iw*min($width/iw\,$height/ih):ih*min($width/iw\,$height/ih), pad=$width:$height:($width-iw*min($width/iw\,$height/ih))/2:($height-ih*min($width/iw\,$height/ih))/2"   IMG_0250_copy.avi;   \ 
)
#------------------------end of file

Once all the videos are ready, one can try to add all the clips together. At the very beginning I tried to use GPL software, like cinelerra, but I found it is still in a premitive stage. so we have to choose a proprietary software, Powerdirector is chosen. 

it is good for transitions and editing. using wmv to output is so far the best choice. to make movie like scene, one can slightly change the color temperature.


 make her head in her pictures (around 10000) translating and scaling right in the middle of the video, then sliding each picture in the video with 0.2 seconds interval. the second chapter is to findout all the interesting scene from our video database


Now the problem of lagging and freezing transition in PowerDirector is solved. it is due to the fact that the source file is encoded as "libx264", which is a higher class coding with better compression and quality. However, as RAW video editor, the file coding should be as RAW as possible rather than as SMALL as possible. Therefore, if one applies MPEG4 as the video encoding, although the file is huge, the editing process becomes ease!!!

ffmpeg -i input -c:v libx264 -preset medium -vpre libx264-ipod640 -crf 24 -c:a libfaac -q:a 100 output.mp4

a very good command to produce file for ipad view, the -crf 24 a value between 18-30 is ok. (30) represents the file is small.


# -vcodec libx264  small but a big laggy
# -vcodec copy     windows can not play
# ffmpeg -i 11library.mp4   -vcodec libxvid -acodec mp2 -ss 00:00:13 -t 00:00:27   11library_libxvid.avi
# ffmpeg -i 11library.mp4   -vcodec mpeg -acodec mp2 -ss 00:00:13 -t 00:00:27   11library_mpeg.avi

## incorrect parameter
# ffmpeg -i 11library.mp4   -vcodec h263 -acodec mp2 -ss 00:00:13 -t 00:00:27   11library_h263.avi
# ffmpeg -i a.avi -s qcif -vcodec h263 -acodec libfaac -ac 1 -ar 44000 -r 25 -ab 32 -y b.3gp




# to play with pmeg4, one has install
#  apt-get install libdvdcss2


## this one works!
# ffmpeg -i 11library.mp4   -vcodec mpeg4  -ac 1 -ar 44100 -r 25 -ab 64  -aspect 4:3 -s xga -y  11library_mpeg4.avi
## -r frame rate 25 frames per
## -ab audio bitrate 64 kb/s

## can we chop, yes we can ,and the good thing is that it is able to crop from video editor as well!
# original file 49mb
# output file 3.4mb for 27 seconds
# ffmpeg -i 11library.mp4   -vcodec mpeg4  -ac 1 -ar 44100 -r 25 -ab 64  -aspect 4:3 -s xga   -ss 00:00:13 -t 00:00:27  -y  11library_mpeg4.avi

# a new try for h264
# output file 7.9mb for 27 seconds
# the file is not editable in video studio although the quanlity is better.
# this file is also not able to run in cinelerra
# ffmpeg -i 11library.mp4   -vcodec h264  -ac 1 -ar 44100 -r 25 -ab 64  -aspect 4:3 -s xga   -ss 00:00:13 -t 00:00:27  -y  11library_h264.avi


# great~~!!!!!!!!! this is the default, which gives the HALF size of video as the original file
# it is ACCEPTABLE in POWERDIRECTER
ffmpeg -i   born.mp4   -vcodec libx264 -ac 1 -ar 44100 -r 29.97 -ab 64 born_default_full.avi
# how about cut in this one
ffmpeg -i   born.mp4   -vcodec libx264 -ac 1 -ar 44100 -r 29.97 -ab 64  -ss 00:00:04 -t 00:00:07  born_default_cut.avi


# how about copy and copy DIRECT COPY IS NOT ACCEPTED IN POWERDIRECTOR 11!!
# same size output
 ffmpeg -i    born.mp4   -vcodec copy -acodec copy  born_copy_full.avi
# how about cut
 ffmpeg -i    born.mp4   -vcodec copy -acodec copy -ss 00:00:04 -t 00:00:07  born_copy_cut.avi


# -vcodec libx264  small but a big laggy
# -vcodec copy     windows can not play (in particular kmp)
# ffmpeg -i 11library.mp4   -vcodec libxvid -acodec mp2 -ss 00:00:13 -t 00:00:27   11library_libxvid.avi
# ffmpeg -i 11library.mp4   -vcodec mpeg -acodec mp2 -ss 00:00:13 -t 00:00:27   11library_mpeg.avi

## incorrect parameter
# ffmpeg -i 11library.mp4   -vcodec h263 -acodec mp2 -ss 00:00:13 -t 00:00:27   11library_h263.avi
# ffmpeg -i a.avi -s qcif -vcodec h263 -acodec libfaac -ac 1 -ar 44000 -r 25 -ab 32 -y b.3gp




# to play with pmeg4, one has install
#  apt-get install libdvdcss2


## this one works!
# ffmpeg -i 11library.mp4   -vcodec mpeg4  -ac 1 -ar 44100 -r 25 -ab 64  -aspect 4:3 -s xga -y  11library_mpeg4.avi
## -r frame rate 25 frames per
## -ab audio bitrate 64 kb/s

## can we chop, yes we can ,and the good thing is that it is able to crop from video editor as well!
# original file 49mb
# output file 3.4mb for 27 seconds
# ffmpeg -i 11library.mp4   -vcodec mpeg4  -ac 1 -ar 44100 -r 25 -ab 64  -aspect 4:3 -s xga   -ss 00:00:13 -t 00:00:27  -y  11library_mpeg4.avi

# a new try for h264
# output file 7.9mb for 27 seconds
# the file is not editable
# ffmpeg -i 11library.mp4   -vcodec h264  -ac 1 -ar 44100 -r 25 -ab 64  -aspect 4:3 -s xga   -ss 00:00:13 -t 00:00:27  -y  11library_h264.avi

# 28 Aug 2013. more study on the video problems.
# the one that is quite compatible with other software is to use "Motion JPEG Video (MJPG) codec". the video by hohai is done by so.
# hohai                                       : Motion JPEG Video (MJPG) codec

# NEXUS Codec (mp4 file from the mobile phone): H264 - MPEG-4 AVC (part 10) (avc1)
#                               Decoded format: Planar 4:2:0 YUV
#                                   frame rate: 30.003560
#                                   resolution: 720*480
#                                 audio codec : MPEG AAC Audio (mp4a)      
#                                  sample rate: 32000 Hz
#                                  
# ffmpeg -vcodec mpeg4 -vb 40M -r 30                        : MPEG-4 Video (FMP4)
# ffmpeg -vcodec libx264:                     : H264 - MPEG-4 AVC (part 10) (H264)   ;   Decoded format: Planar 4:2:0 YUV  ; frame rate:                                      audo codec  : MPEG Audio layer 1/2/3 (mpga); sample rate: 44100 Hz
# ffmpeg -vcodec h264                         : H264 - MPEG-4 AVC (part 10) (H264)   ;   Decoded format: Planar 4:2:0 YUV  ; frame rate: 30.083334  ; resolution: 720*480
# ffmpeg -copy (from NEXUS)                   : (same as nexus)
# nokia E71                                   :

# Iphone 4s
# Iphone 4s              : H264 -MPEG-4 AVC (part10) (avc1)
#          Decoded format: Planar 4:2:0 YUV
#              frame rate: 24.129286
#             sample rate: 44100 Hz
#              resolution: 1920*1080
#             audio codec: MPEG AAC Audio (mp4a)
#             sample rate: 44100 Hz
#           input bitrate: 21741 kb/s   (value changes with time)
#         content bitrate: 25708 kb/s   (value changes with time)

# H264 is now enshrined in MPEG4 as part 10 also known as AVC
# see page http://stackoverflow.com/questions/10477430/what-is-the-difference-between-h-264-video-and-mpeg-4-video
#

No comments:

Post a Comment