Skip to content

Latest commit

 

History

History
216 lines (122 loc) · 6.44 KB

ffmpeg.md

File metadata and controls

216 lines (122 loc) · 6.44 KB

ffmpeg

CLI front-end for the FFmpeg library. Can do tons of conversions and streaming.

Tested on Ubuntu 15.10.

List codecs: http://stackoverflow.com/questions/3377300/what-are-all-codecs-supported-by-ffmpeg

ffmpeg -codecs

stdin and stdout operations

ffmpeg can detect file types from both file names and contents. But if you want to be explicit, remember the man ffmpeg signature:

ffmpeg [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ...

So the -f before -i is for the input format, and the -f after -i is the output.

Example: stream your desktop and play it:

ffmpeg -video_size 640x480 -framerate 25 -f x11grab -i :0.0+100,200 -f h264 - | ffplay -f h264 -

Extract raw streams

VP9

Freedom!

ffmpeg -framerate 4 -pattern_type glob -i '*.jpeg' -c:v vp9 out.webm

TODO: quality is bad.

H264

Works:

ffmpeg -i in.mp4 -vcodec copy -bsf h264_mp4toannexb -an -f h264 out.h264

VLC cannot open that output file, but ffplay can. TODO: is the -bsf really needed?

ffmpeg -i in.mts -vcodec copy -an -f h264 out.h264

AAC

http://superuser.com/questions/186465/extract-audio-aac-from-mp4

ffmpeg -i in.mp4 -c copy -map 0:a:0 out.aac

Webcam

https://trac.ffmpeg.org/wiki/Capture/Webcam

First install v4l2, then:

ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video0 -f v8 output.mkv

ffprobe

Get information about video and audio files:

ffprobe a.webm

Get frame count

Not easy because it is not stored on the video, it requires decoding.

Get index of all key frames

http://superuser.com/questions/885452/extracting-the-index-of-key-frames-from-a-video-using-ffmpeg

ffprobe -select_streams v \
    -show_frames \
    -show_entries frame=pict_type \
    -of csv \
    tmp.h264

generates something like:

frame,I
frame,B
frame,P
frame,B
frame,P
frame,B
frame,P
frame,B
frame,P
frame,B
frame,I

where I, B, and P are defined at: https://en.wikipedia.org/wiki/Video_compression_picture_types

From there on, Bash it up.

Images to video

Just works:

ffmpeg -framerate 1 -pattern_type glob -i '*.png' -c:v libx264 -r 30 -pix_fmt yuv420p out.mp4

-r 30 to use a standard frame rate and be more compatible, see: http://stackoverflow.com/questions/19267443/playback-issues-in-vlc-with-low-fps-video-from-images-using-ffmpeg/41797724#41797724

Smooth transitions

ffplay

Minimalistic video preview tool.

ffplay video.mp4

No controls, no nothing, just a window with the video.

Exit when file is over: http://ffmpeg-users.933282.n4.nabble.com/ffplay-does-not-exit-automatically-after-the-file-has-completed-playing-td2969254.html

ffplay -autoexit video.mp4

Loop infinitely:

ffplay -loop -1 video.mp4

Raw audio file:

ffplay -autoexit -f u16be -ar 44100 -ac 1 in.raw

Stream

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

ffserver

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

TODO: what is the IO format: RTMP of HTTP? How is video sent over HTTP?

Note: this project seems very unmaintained, and there are better ones out there.

Server configuration file:

/etc/ffserver.conf

Use a custom one and show server debug:

ffserver -d -f ~/ffserver.conf

Start server:

ffserver

Send video to it. Must edit the VideoFrameRate to something larger than 3... https://lists.ffmpeg.org/pipermail/ffmpeg-user/2016-January/030111.html:

ffmpeg -f v4l2 -s 320x240 -r 25 -i /dev/video0 -f alsa -ac 2 -i hw:1 http://localhost:8090/feed1.ffm

Watch it. Must edit ACL inside feed http://stackoverflow.com/a/13977181/895245 or else access denied:

ffplay http://localhost:8090/test1.mpg

TODO: video has a huge delay from starting up to playing, see: https://trac.ffmpeg.org/wiki/StreamingGuide#Latency

A few URLs that can be accessed from the browser:

udp protocol

An FFmpeg invention it seems: http://stackoverflow.com/questions/27930879/what-is-ffmpegs-udp-protocol

Trim by time

Cut by time

Best command:

ffmpeg -i in.ogv -ss 00:00 -to 03:30 -c copy out.ogv

Concatenate

Did not upload correctly to YouTube in 2018:

ffmpeg -i concat:"in1.ogv|in2.ogv" -c copy out.ogv

but the other method did:

ffmpeg -i in1.ogv -i in2.ogv -filter_complex "[0:v] [0:a] [1:v] [1:a] concat=n=2:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" out.ogv

Merge audio and video

http://superuser.com/questions/277642/how-to-merge-audio-and-video-file-in-ffmpeg

Yeah, this works on VLC and YouTube:

ffmpeg -i video.mp4 -i audio.mp3 -c copy output.mkv