Tag: FFmpeg

  • How to Install ffmpeg static build

    How to Install ffmpeg static build

    FFmpeg binary static build available for download from

    https://johnvansickle.com/ffmpeg/

    To install ffmpeg static build, run

    cd /usr/local/src
    wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz
    tar xvf ffmpeg-release-amd64-static.tar.xz
    cd ffmpeg-*-amd64-static/
    cp ffmpeg ffprobe qt-faststart /usr/bin/
    cp -r model /usr/local/share/
    

    See ffmpeg

  • How to take screenshot with ffmpeg

    How to take screenshot with ffmpeg

    ffmpeg provides x11grab driver that can be used to take screenshots.

    You can use the following command to take screenshots, you can run it manually on the terminal or using cronjob

    export DISPLAY=:1
    ffmpeg -f x11grab -video_size 1920x1080 -i $DISPLAY -vframes 1 image.png
    

    If you want to make a thumbnail from a video, you can use

    ffmpeg -ss 00:01:00 -i video.mp4 -vframes 1 image.png
    

    You can change 00:01:00 to whatever time you need to generate a thumbnail.

    See ffmpeg

  • Steaming to Anti Media Server using ffmpeg

    To team a video io Anti Media Server using ffmpeg command line, use

    ffmpeg -re -i VIDEO_FILE.mp4 -codec copy -f flv ANT_MEDIA_SERVER_RTMP_URL
    

    Example

    ffmpeg -re -i video.mp4 -codec copy -f flv rtmp://stream.serverok.in/LiveApp/225873992721868053400782
    

    Cammand with more steam option

    ffmpeg -re -i 'video.mp4'  -c:v libx264 -b:v 1600k -preset ultrafast -b 900k -c:a aac -b:a 128k -s 1920x1080 -x264opts keyint=50 -g 25 -pix_fmt yuv420p -f flv rtmp://stream.serverok.in/LiveApp/225873992721868053400782
    

    See Ant Media Server

  • Show all formats supported by ffmpeg

    To get list of video format supported by ffmpeg, run

    ffmpeg -formats
    

    Example

    ffmpeg formats

    See ffmpeg

  • check if ffmpeg support H264

    To check if ffmpeg installed on your computer support h264, run

    ffmpeg -formats | grep h264
    

    ffmpeg h264 support

    See ffmpeg

  • Extract Audio from Video using ffmpeg

    To extract Audio from Video using ffmpeg, run

    ffmpeg -i VIDEO_FILE.mp4 audio.mp3
    
  • Convert Video info MP4 using ffmpeg

    Convert Video For Streaming

    ffmpeg -y -i FILE_NAME.wmv -c:v libx264 -c:a aac -coder 1 -b:v 500k -threads 0 -b:a 96k FILE_NAME.mp4

    if you want to scale the video to a specific size, use -vf scale option, for example

    ffmpeg -y -i FILE_NAME.wmv -vf scale=720:480 -c:v libx264 -c:a aac -coder 1 -b:v 500k -threads 0 -b:a 96k FILE_NAME.mp4

    Inject metadata

    qt-faststart FILE_NAME.mp4 FILE_NAME-meta.mp4

    Convert Video to MP4

    This is verified method, and works good, i converted many videos, no quality lose

    ffmpeg  -i FILE_NAME.INPUT_EXTN -crf 25.0 -c:v libx264 -c:a libfaac -ar 48000 -b:a 160k -coder 1 -threads 0 FILE_NAME_nm.mp4

    If you get error related to libfaac, try changing it with aac.

    To cut a video during transcoding

    time ffmpeg -ss 00:34:00 -t 00:02:00 -i FILE_NAME.INPUT_EXTN -crf 25.0 -c:v libx264 -c:a libfaac -ar 48000 -b:a 160k -coder 1 -threads 0 FILE_NAME_nm.mp4

    Other Methods, Need to experiment

    To convert video to H.264 (mp4) format

    ffmpeg -i 'input.mpg' -acodec libfaac -ab 96k -vcodec libx264 -vpre hq -crf 22 -threads 0 output.mp4
    ffmpeg -i 'input.mpg' -vcodec libx264 -vpre hq -b 1M -bt 1M -threads 0 outfile.mp4
    ffmpeg -i test.avi -acodec libfaac -ab 128k -ar 44100 -s 704x400 -r 20 -vcodec libx264 -b 256000 -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me_method umh -subq 5 -trellis 1 -refs 2 -bf 1 -coder 1 -me_range 16 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 256000 -maxrate 4M -bufsize 4M -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 21 test.mp4
    ffmpeg -y -i source.avi -vcodec libx264 -b BITRATE -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -flags2 +mixed_refs -me umh -subq 5 -trellis 1 -refs 5 -bf 3 -b_strategy 1 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt "$vbitrate" -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 41 -acodec libfaac -ac 2 -ar 48000 -ab "$abitrate" -f mov out.mov

    Using ffmpeg and x264

    Example two-pass encoding script:

    #!/bin/bash
    # Two-Pass x264 Encoding Script
    # Usage: ./264encode.sh input output.mp4
    
    # Pass 1
    ffmpeg -y -i $1 -pass 1 -b 512k -bt 512k -vcodec libx264 -an -threads 0 -coder 1 -flags +loop -cmp +chroma -partitions -parti8x8-parti4x4-partp8x8-partp4x4-partb8x8 -me_method dia -subq 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -bf 16 -refs 1 -directpred 1 -bidir_refine 0 -trellis 0 -flags2 -bpyramid-wpred-mixed_refs-dct8x8+fastpskip -f mp4 /dev/null
    
    # Pass 2
    ffmpeg -y -i $1 -pass 2 -b 512k -bt 512k -vcodec libx264 -acodec libfaac -ab 128k -ac 2 -threads 0 -coder 1 -flags +loop -cmp +chroma -partitions +parti8x8+parti4x4+partp8x8+partb8x8 -me_method umh -subq 8 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -bf 16 -refs 4 -directpred 3 -trellis 1 -flags2 +bpyramid+wpred+mixed_refs+dct8x8+fastpskip -f mp4 $2

    To use

    sh 264encode.sh input.avi output.mp4

    See ffmpeg

  • Create video thumbnail using ffmpeg/mplayer

    Create thumbnail with ffmpeg

    To create thumbnail from video using ffmpeg, use

    ffmpeg -y -ss HH:MM:SS -i VIDEO.AVI -f image2 -vframes 1 1.jpg
    

    Here

    HH:MM:SS = replace with time where you want to take screen shot.

    1.jpg = is the tumbnail image generated.

    Create thumbnail with mplayer

    METHOD 1

    mkdir out
    mplayer VIDEO.AVI -ss 0:8:0 -nosound -vo jpeg:outdir=out -frames 2
    

    -ss = you can specify time in seconds or in hh:mm:ss format.

    -frames 2 = create 2 thumbnails with name 00000001.jpg and 00000002.jpg

    -ss 0:8:0 = thumbnail from 8th minutes

    METHOD 2

    mencoder -ss 00:25:00 -endpos 0.001 -ovc copy -nosound VIDEO.AVI -o tmpfile1
    mplayer -nosound -vo jpeg tmpfile1
    

    Create thumbnail with avconv

    avconv -ss 00:01:00 -i VIDEO_FILE.mp4 -f image2 -frames:v 1 1.jpeg
    

    This will create thumbnail 1.jpeg from video.

    -ss 00:01:00 – Time from where image is taken.

    See ffmpeg

  • Scale a video using ffmpeg

    To Scape a Video using ffmpeg, run

    ffmpeg -i FILE_NAME.avi -vf scale=1280:720 FILE_NAME_OUT.avi
    

    See ffmpeg

  • Cut a Video using ffmpeg

    To cut a video using ffmpeg, use

    ffmpeg -ss 00:17:00 -i IN_FILE.avi -t DURATION_IN_SECONDS -acodec copy -vcodec copy -async 1 OUT_FILE.avi
    

    To cut a video and encoding it

    ffmpeg -ss 00:17:00 -i IN_FILE.avi -t DURATION_IN_SECONDS -crf 25.0 -c:v libx264 -c:a aac -ar 48000 -b:a 160k -coder 1 -threads 0 OUT_FILE.mp4
    

    DURATION_IN_SECONDS specify duration of new clip. If you specify 60, it will cut 60 seconds of video start from time specified in -ss. In this cause, from 00:17:00 to 00:18:00.

    For more info, see “Cutting small sections” part of ffmpeg documentation.

    https://trac.ffmpeg.org/wiki/Seeking

    ffmpeg -ss 00:34:00 -t 00:02:00 -i IN_FILE.wmv -crf 25.0 -c:v libx264 -c:a libfaac -ar 48000 -b:a 160k -coder 1 -threads 0 OUT_FILE.mp4
    

    In this example

    -ss 0:34:00 -t 0:02:00
    

    Means cut video from 00:34:00 to 00:36:00

    If you want to cut a video from time 01:00:00 to 01:20:00, use

    -ss 01:00:00 -t 00:20:00
    

    10-Apr-2013 This command works great, i tested on many videos.

    Cut video with avconv

    avconv -y -ss 00:04:00 -i BootstrapWebDesign-03.mov -t 00:00:30 -crf 25.0 -c:v libx264 -c:a libvo_aacenc -ar 48000 -b:a 160k -coder 1 -threads 0 1.mp4
    

    Cut video with mencoder

    mencoder -ss 01:00:00 -endpos 00:00:30 -ovc copy -oac copy 1.avi -o 2.avi
    

    -ss = start time in hh:mm:ss format

    -endpos = Duration in hh:mm:ss format

    Example

    Following with create a video “out.avi” from MSFT.avi from 00:24:00 to 00:25:00

    mencoder -ss 00:24:00 -endpos 00:01:00 -ovc copy -oac copy MSFT.avi -o out.avi
    

    21 April 2011 verified and working

    See ffmpeg

  • Install ffmpeg on Ubuntu

    To install ffmpeg 4 on Ubuntu, you can use PPA

    https://launchpad.net/~jonathonf/+archive/ubuntu/ffmpeg-4

    apt install software-properties-common -y
    add-apt-repository ppa:jonathonf/ffmpeg-4 -y
    

    Now install ffmpeg with

    apt update && apt install ffmpeg -y
    

    See ffmpeg

  • error: ‘x264_bit_depth’ undeclared (first use in this function)

    When installing ffmpeg, i get error

    CC	libavcodec/libvorbisenc.o
    CC	libavcodec/libx264.o
    libavcodec/libx264.c: In function ‘X264_frame’:
    libavcodec/libx264.c:282:9: error: ‘x264_bit_depth’ undeclared (first use in this function)
         if (x264_bit_depth > 8)
             ^
    libavcodec/libx264.c:282:9: note: each undeclared identifier is reported only once for each function it appears in
    libavcodec/libx264.c: In function ‘X264_init_static’:
    libavcodec/libx264.c:892:9: error: ‘x264_bit_depth’ undeclared (first use in this function)
         if (x264_bit_depth == 8)
             ^
    make: *** [libavcodec/libx264.o] Error 1
    

    This is due to incompatability with ffmpeg and x264.

    What you can do is try another version of x264. You will be able to download older version of x264 from

    ftp://ftp.videolan.org/pub/x264/snapshots

    On 2018-08-14, it failed with latest stable snapshot. So i tried 14 days old stable snapshot

    cd /usr/local/src/
    wget ftp://ftp.videolan.org/pub/x264/snapshots/x264-snapshot-20180730-2245-stable.tar.bz2
    tar xvf x264-snapshot-20180730-2245-stable.tar.bz2
    cd /usr/local/src/x264-snapshot-20180730-2245-stable
    make clean && make distclean
    ./configure --prefix=/usr --enable-shared
    make && make install
    ldconfig
    

    This worked with ffmpeg.

    Solution here is to try latet version first, if it did not work, try older version, say try 1 month old version, if that did not work, go back 2 months, see if that works.

    Now install ffmpeg with

    cd /usr/local/src/
    wget https://github.com/FFmpeg/FFmpeg/archive/n3.3.5.tar.gz
    tar zxf n3.3.5.tar.gz
    cd /usr/local/src/FFmpeg-n3.3.5/
    ldconfig
    make clean && make distclean
    ./configure --prefix=/usr --enable-shared --enable-libxvid --enable-libvorbis --enable-libtheora --enable-libmp3lame --enable-gpl --enable-libfdk-aac --enable-nonfree --enable-libx264 --enable-libfreetype
    make && make install && ldconfig