ffmpeg

Example 0: just probing to see what the video contains, and what size it is, as well as how long:
ffmpeg -i VTS_02_1.VOB

Input #0, mpeg, from ‘VTS_02_1.VOB’:
Duration: 00:23:47.39, start: 0.280633, bitrate: 6017 kb/s
Stream #0:0[0x1bf]: Data: dvd_nav_packet
Stream #0:1[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 720×480 [SAR 32:27 DAR 16:9], max. 9800 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:2[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels, s16, 1536 kb/s

(You might get an “error” or “warning” message about missing output file names, but that’s tamed. just ignore it.)
IF you get any questions about “overwriting” files, likely due to you mistyping something.., just say “N” for no to exit.
—————
Example 1: Simple task of extracting a clip from a video

Let’s say you have a video called big_file.mp4 that is 3hours long, and you want to cut out Act 1. First, play on VLC to find out where Act1 begins and ends. The time format is listed as HH:MM:SS. Here Let us assume Act 1 starts at 00:05:59 and ends at 01:34:04. Next is to calculate the duration of Act 1:
01:34:04 minus 00:05:59 = 01:33:64 minus 00:05:59 = 01:28:05

Below are 3 cases. The syntax in general is:
ffmpeg -i input_video -acodec [codecname] -vcodec [codecname] -ss HH:MM:SS -t HH:MM:SS output_video
where
-i = input
-acodec = audio codec, see list in Example 8 below, typical options: “libmp3lame”, “libfaac”
-vcodec = video codec , options: i typically just use “copy” or “libx264”
-ss = start time
-t = duration

Case1: keep all video and audio codec the same as the original video:
ffmpeg -i big_file.mp4 -acodec copy -vcodec copy -ss 00:05:59 -t 01:28:05 file_Act1.mp4

Case2: keep audio format and compress video to save disk space but keep quality very similar, note file format will change to mkv
ffmpeg -i big_file.mp4 -acodec copy -vcodec libx264 -ss 00:05:59 -t 01:28:05 file_Act1.mkv

Case3: change also audio codec (I general do not recommend because i don’t want to alter audio in any form, however, sometimes audio comes in skipping or have problems.. then we also convert audio)
ffmpeg -i big_file.mp4 -acodec libfaac -vcodec libx264 -ss 00:05:59 -t 01:28:05 file_Act1.mkv (note i just pick libfaac here, but can also use libmp3lame which actually is worse than libfaac)
—————–
Example2: convert VOB to mkv and keeping the subtitles, then merge files together:

Sometimes, the VOB files are scrambled, such that every VOB in the above list has video, audio, and subtitles on different channels.  For example, for 1 of the cases I had, the following are in VTS_01_1.VOB and VTS_01_2.VOB:

ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i VTS_01_1.VOB

Input #0, mpeg, from ‘VTS_02_1.VOB’:
Duration: 00:23:47.39, start: 0.280633, bitrate: 6017 kb/s
Stream #0:0[0x1bf]: Data: dvd_nav_packet
Stream #0:1[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 720×480 [SAR 32:27 DAR 16:9], max. 9800 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:2[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels, s16, 1536 kb/s
Stream #0:3[0x20]: Subtitle: dvd_subtitle
Stream #0:4[0x21]: Subtitle: dvd_subtitle

ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i VTS_01_2.VOB

Input #0, mpeg, from ‘VTS_01_2.VOB’:
Duration: 00:19:55.26, start: 1427.313967, bitrate: 7186 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 720×480 [SAR 32:27 DAR 16:9], max. 9800 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels, s16, 1536 kb/s
Stream #0:2[0x1bf]: Data: dvd_nav_packet
Stream #0:3[0x20]: Subtitle: dvd_subtitle
Stream #0:4[0x21]: Subtitle: dvd_subtitle
Stream #0:5[0x22]: Subtitle: dvd_subtitle
Stream #0:6[0x23]: Subtitle: dvd_subtitle
Stream #0:7[0x24]: Subtitle: dvd_subtitle

That is just downright annoying, because we can’t simply merge the VOB’s . So I’d extract which ever subtitles, as well as audio and video for each individual files, e.g., for the 2 above:
ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i VTS_01_1.VOB -map 0:1 -vcodec libx264 -map 0:2 -acodec pcm_s24le -metadata:s:a:0 language=eng -map 0:3 -scodec copy -metadata:s:s:0 language=eng -map 0:7 -scodec copy -metadata:s:s:1 language=ita vts_01_1.mkv

ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i VTS_01_2.VOB -map 0:0 -vcodec libx264 -map 0:1 -acodec pcm_s24le -metadata:s:a:0 language=eng -map 0:3 -scodec copy -metadata:s:s:0 language=eng -map 0:7 -scodec copy -metadata:s:s:1 language=ita vts_01_2.mkv

So now, they’re consistent in that 0:0 is video, 0:1 is audio, 0:2 is subtitle, and 0:3 is subtitle. To find out which languages they are, normally i just open the VOB’s in VLC and cycle through, for example, for this example above, i found out the included languages are:

1=eng 0:3
2=ger 0:4
3=fre 0:5
4=spa 0:6
5=ita 0:7

But note that it can be different orders for each VOB (i think they purposely do this). In any case, after converting them, to mkv, you can now merge them using by creating a >mylist.txt files looks like this:

file ‘vts_01_1.mkv’
file ‘vts_01_2.mkv’

Then merge:
ffmpeg -f concat -i mylist.txt -c:v copy -c:a copy -c:s copy -map 0:1 -map 0:2 -map 0:3 -map 0:4 output.VOB
where 0:1 is the video, 0:2 is the audio, 0:3 and 0:4 are the two subtitles we keep.
—————–
Example3: merge a list of individual VOB files together:
If the VOB files are well behaved (i.e., every file has 0:0 for video, 0:1 for audio, 0:2 for same subtitles), I’d have rather just merge them together first to create a huge big_files.VOB, then convert them to mkv to save space. This way we exclude the hiccups that happens when you do Example 1.

%probing for subtitles:
ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i VTS_01_1.VOB

#Input #0, mpeg, from ‘VTS_01_1.VOB’:
# Duration: 00:18:17.70, start: 0.300300, bitrate: 7824 kb/s
# Stream #0:0[0x1bf]: Data: dvd_nav_packet
# Stream #0:1[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 720×480 [SAR 32:27 DAR 16:9], max. 7200 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
# Stream #0:2[0x81]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 448 kb/s
# Stream #0:3[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels, s16, 1536 kb/s
# Stream #0:4[0x20]: Subtitle: dvd_subtitle
# Stream #0:5[0x21]: Subtitle: dvd_subtitle
# Stream #0:6[0x22]: Subtitle: dvd_subtitle
# Stream #0:7[0x23]: Subtitle: dvd_subtitle

for i in {1..10}; do printf “file ‘%s’\n” vts_01_1.VOB >> mylist.txt;done (this prints vts_01_1.VOB 10 times)

then edit the file mylist.txt such that it’s as followed:

file ‘vts_01_1.VOB’
file ‘vts_01_2.VOB’
file ‘vts_01_3.VOB’
file ‘vts_01_4.VOB’
file ‘vts_01_5.VOB’
file ‘vts_01_6.VOB’
file ‘vts_01_7.VOB’
file ‘vts_01_8.VOB’

save the file, exit. then execute the ffmpeg line below keeping only audio (ac3) and video:

ffmpeg -f concat -i mylist.txt -c:v copy -c:a copy -map 0:1 -map 0:2 output.VOB

or add some more flags below to  keep also subtitles (which is quite tricky):
ffmpeg -f concat -i mylist.txt -c:v copy -c:a copy -c:s copy -map 0:1 -map 0:2 -map 0:4 output.VOB

Explanation: -map INPUT:CHANNEL shows which channel from the input stream is being mapped. so -c:v copy -map 0:1 says to copy the video codec from the original, where video is listed as “Stream #0:1[0x1e0]: Video:” . Similarly, -c:a copy -map 0:2 says to copy video codec and take it from tream #0:2[0x81]: Audio: ac3, 48000 Hz. Note there’s an extra audio on “# Stream #0:3[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels,”, so can choose to keep this instead because it’s stereo, because the ac3 one appears to be surround sound (5.1side). syntax for keeping pcm_dvd sound would be:

ffmpeg -f concat -i mylist.txt -c:v copy -c:a copy -c:s copy -map 0:1 -map 0:3 -map 0:4 output.VOB

Finally, if want to keep more than 1 subtitles, then use more mapping, e.g.,

ffmpeg -f concat -i mylist.txt -c:v copy -c:a copy -c:s copy -map 0:1 -map 0:3 -map 0:4 -map 0:5 output.VOB
—————–
Example4: separate audio and video
ffmpeg -i file.mp4 -an -vcodec copy file_video.mp4
ffmpeg -i file.mp4 -vn -acodec copy file_audio.mp4
—————–
Example5: take audio and/or video from different sources:
ffmpeg -i video.mp4 -i video2.mp4 -map 0:0 -vcodec copy -map 1:1 -acodec copy merge.mp4
—————–
Example6: delay audio (for case when audio comes before video; put the delay time just before -i)
ffmpeg -i file_video.mp4 -ss 00:00:00.4 -i file.mp3 -vcodec copy -acodec copy merge.mkv
OR
below: audio comes before video, need to delay audio
ffmpeg -i file.mp4 -itsoffset 0.3 -i file.mp4 -map 0:v -map 1:a -vcodec copy -acodec copy -bsf:a aac_adtstoasc file_p3.mp4
if video comes before audio and want to delay video:
ffmpeg -i file.mp4 -itsoffset 0.3 -i file.mp4 -map 1:v -map 0:a -vcodec copy -acodec copy -bsf:a aac_adtstoasc file_p3.mp4
Note: itoffset is applied to the 2nd call of file.mp4, which corresponds to map 1 , so that’s the track being delayed.
Example7: produce video from single pix + audio
ffmpeg -loop 1 -i pix.png -i track.mp3 -vcodec libx264 -acodec copy -ss 00:10:25 -t 00:04:38 track.mkv
—————–
Example8: codec list
audio: libmp3lame, libfaac,
video: libx264
(also, something to fix audio synching issues? -bsf:a aac_adtstoasc)
—————–
Example9: cropping:
ffmpeg -i input.mp4 –> gives video size 1280×720, now want to crop 140px off on the right and 60px vertical, distributed as cut 30px from top and 30px from bottom:
syntax for crop: -filter:v “crop=width:height:px_x_0:px_y_0”
where [px_x_0,px_y_0] is the upper right corner
ffmpeg -i input.mp4 -acodec copy -filter:v “crop=1140:580:0:30” output.mp4
—————–
Example10: scaling
ffmpeg -i temp2.mp4 -vf scale=800:-1 temp3.mp4
the -1 keeps aspect ratio.  If want an exact video size instead of preserving aspect ratio, then specify as -vf scale=800:400 for width=800:height=400
—————–
Example11: stretching in length of video:
ffmpeg -i temp3.mp4 -filter:v “setpts=1.2*PTS” temp4.mp4
—————–
probe for available devices on a mac:
ffmpeg -f avfoundation -list_devices true -i “” (there are 2 double quotes at the end)
[AVFoundation input device @ 0x7f9942c227e0] AVFoundation video devices:
[AVFoundation input device @ 0x7f9942c227e0] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7f9942c227e0] [1] Capture screen 0
[AVFoundation input device @ 0x7f9942c227e0] [2] Capture screen 1
[AVFoundation input device @ 0x7f9942c227e0] AVFoundation audio devices:
[AVFoundation input device @ 0x7f9942c227e0] [0] Soundflower (2ch)
[AVFoundation input device @ 0x7f9942c227e0] [1] Built-in Microphone
[AVFoundation input device @ 0x7f9942c227e0] [2] Soundflower (16ch)

now syntax:
ffmpeg -f avfoundation -i “video:audio” out.mkv
ffmpeg -f avfoundation -i “1:0” out.mkv
—————–
ExampleXX: resizing + rescaling (with much more complications)
STEP 0. probing original video test.VOB to obtain information:

ffmpeg -i test.VOB

Input #0, mpeg, from ‘test.VOB’:
Duration: 00:05:00.08, start: 1.000000, bitrate: 9573 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p, 720×576
[SAR 16:15 DAR 4:3], 9200 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x80]: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s

mplayer test.VOB

Movie-Aspect is 1.33:1 – prescaling to correct movie aspect.
VO: [corevideo] 720×576 => 768×576 Packed YUY2

STEP 1. So, test.VOB has original size 768×576 but is shoved into
a display size 720×576. But a view of this video
(video768x576.png) shows that this is not the original
size, hence 2 black bars are put at top and bottom. To
crop to size 768×432 (display 720×432), (video768x432.png)
the command is:
ffmpeg -i test.VOB -acodec copy -vf crop=720:432:0:64 -q:v 0 test1.VOB
ffmpeg -i test1.VOB

Input #0, mpeg, from ‘test1.VOB’:
Duration: 00:05:00.08, start: 1.000000, bitrate: 7800 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p, 720×432
[SAR 16:15 DAR 16:9], 104857 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x80]: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s

STEP 2A. Now scale up to original size 1024×576 (display size 720:576):
(video1024x576.png)

ffmpeg -i test1.VOB -acodec copy -vf scale=720:576 -q:v 0 test3.VOB
ffmpeg -i test3.VOB
Input #0, mpeg, from ‘test3.VOB’:

Duration: 00:05:00.08, start: 1.000000, bitrate: 9191 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p, 720×576
[SAR 64:45 DAR 16:9], 104857 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x80]: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s

mplayer test2.VOB

VO: [corevideo] 720×576 => 1024×576 Packed YUY2

STEP 2B. OR scale up even higher because video quality “allows”:
(video1366x768.png); allow = w/o looking pixelated.
ffmpeg -i test1.VOB -acodec copy -vf scale=1024:768 -q:v 0 test2.VOB
mplayer test2.VOB

VO: [corevideo] 1024×768 => 1366×768 Packed YUY2

STEP 3. now compress video using most efficient codec libx264 to reduce size:
ffmpeg -i test3.VOB -acodec copy -vcodec libx264 test3.mkv
ffmpeg -i test3.mkv

Duration: 00:05:00.12, start: 0.000000, bitrate: 1791 kb/s
Stream #0:0: Video: h264 (High), yuv420p, 720×576
[SAR 64:45 DAR 16:9], 25 fps, 25 tbr, 1k tbn, 50 tbc (default)
Stream #0:1: Audio: ac3, 48000 Hz, stereo, s16, 256 kb/s (default)

mplayer test3.mkv

VO: [corevideo] 720×576 => 1024×576 Packed YUY2

Example13: probing for and keeping subtitles
%probing:
ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i VTS_01_1.VOB

#Input #0, mpeg, from ‘VTS_01_1.VOB’:
# Duration: 00:18:17.70, start: 0.300300, bitrate: 7824 kb/s
# Stream #0:0[0x1bf]: Data: dvd_nav_packet
# Stream #0:1[0x1e0]: Video: mpeg2video (Main), yuv420p(tv), 720×480 [SAR 32:27 DAR 16:9], max. 7200 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
# Stream #0:2[0x81]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 448 kb/s
# Stream #0:3[0xa0]: Audio: pcm_dvd, 48000 Hz, 2 channels, s16, 1536 kb/s
# Stream #0:4[0x20]: Subtitle: dvd_subtitle
# Stream #0:5[0x21]: Subtitle: dvd_subtitle
# Stream #0:6[0x22]: Subtitle: dvd_subtitle
# Stream #0:7[0x23]: Subtitle: dvd_subtitle

ffmpeg -i VTS_02_1.VOB -vcodec libx264 -acodec pcm_s24le test.mkv

ffmpeg -analyzeduration 2147483647 -probesize 2147483647 -i VTS_02_1.VOB -map 0:1 -vcodec libx264 -map 0:2 -acodec pcm_s24le -metadata:s:a:0 language=eng -map 0:3 -scodec copy -metadata:s:s:0 language=eng -map 0:7 -scodec copy -metadata:s:s:1 language=ita vts_02_1.mkv

%now merging and keeping subtitles:
for i in {1..10}; do printf “file ‘%s’\n” vts_02_1.mkv >> mylist.txt;done

then edit the file mylist.txt such that it’s as followed:

file ‘vts_02_1.mkv’
file ‘vts_02_2.mkv’
file ‘vts_02_3.mkv’
file ‘vts_02_4.mkv’
file ‘vts_02_5.mkv’
file ‘vts_02_6.mkv’
file ‘vts_02_7.mkv’
file ‘vts_02_8.mkv’

now merging:
ffmpeg -f concat -i mylist.txt -c:v copy -c:a copy -c:s copy -map 0:0 -map 0:1 -map 0:2 -map 0:3 output.mkv
—————–
Example14: produce slide-show from multiple pix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: