Movie Making & Editing by T.A. Gonsalves ================= Last edited: 8 Aug 2020 This file contains notes on assorted techniques for making and manipulating simple videos. ---------------------------------------------------------------- Contents ======== 1. D7200 Interval Timer Photography 2. ffmpeg 2.1. Format conversion 2.2. File size 2.3. Merging video and audio 2.4. Concatenating Files 2.5 Editing Audio 3. Screen recording on MacBook 3.1. Self-video using iPad ---------------------------------------------------------------- 1. D7200 Interval Timer Photography =================================== 15sec interval and framerate 12 (ffmpeg) ==> 180x gives fairly smooth result. Camera must be on very sturdy tripod else buildings shake. 15s and framerate 25 ==> 375x similar to above 2. ffmpeg ========= > ffmpeg -framerate 5 -i D72_15%3d.JPG video.avi ffmpeg assumes images numbered starting from 0-3 consecutively without gaps > ffmpeg -framerate 5 -start_number 85 -i D72_15%3d.JPG video.avi > ffmpeg -framerate 10 -start_number 121 -i D72_15%3d.JPG -vframes 100 video.avi > ffmpeg -framerate 8 -i img_%03d.JPG -s 720x480 Moonrise_08.avi > ffmpeg -framerate 8 -i img_%03d.JPG -s 960x640 Moonrise_08.avi 2.1. Format conversion ---------------------- > ffmpeg -i Moonrise_08.avi Moonrise_08.webm > ffmpeg -i Moonrise_08.avi Moonrise_08.mp4 Safari requires HEVC which is a variant of MP4. ffmpeg -i input.mov -c:v libx264 -profile:v main -vf format=yuv420p -c:a aac -crf 10 -movflags +faststart output.mp4 crf: 0 -- lossless compression. <18 -- visually lossless. 28 -- very good compression with some visible loss 2.2. File size -------------- > ffmpeg -t 669 -i OCEANS22_WIE_TAG_23Feb22_rec.mp4 -t 669 -i OCEANS22_WIE_TAG_23Feb22_rec.mp4 -s 960x540 -f webm -vcodec libvpx -crf 28 -acodec libvorbis -ar 8000 -map 0:v:0 -map 1:a:0 output.webm WEBM is better supported than MP4 in browsers and has a 50% smaller file size. > ffmpeg -i input.mp4 -c:v libx265 -crf 28 output.mp4 H.265 codec with compression factor 24-30 gives 70 MB file size from 220 MB, but colour almost disappears, mostly b&w. crf = 50: 1.2 MB, but colour almost disappears, mostly b&w. crf = 40: 9 MB, but colour almost disappears, mostly b&w. crf = 35: 23 MB, but colour almost disappears, mostly b&w. crf = 30: 51 MB, but colour almost disappears, mostly b&w. Problem is with libx265 codec. > ffmpeg -i input.mp4 -crf 28 output.mp4 Good output, filesize 6MB crf = 50: 123 KB, splotchy output crf = 40: 1 MB, decent output 4K .mp4 shot on a Sony RX100m6 -- about 7.5 MB/sec > ffmpeg -i C0009.MP4 -s 800x450 -r 16 -crf 28 -ar 8000 C0009_lores.mp4 About 0.15 MB/sec (50x file size reduction), with acceptable quality. 2.3. Merging video and audio ---------------------------- > ffmpeg -i LS_01.mp4 -i LS_01.m4a -s 640x480 -r 12 -crf 40 -c:a aac -ar 8000 -map 0:v:0 -map 1:a:0 LS_01_small.mp4 > ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 output.mp4 Assumes that the video file does not contain any audio stream yet, and input/output formats same. Transcodes the audio, since MP4s cannot carry PCM audio streams. For other audio codecs, see the FFmpeg Wiki: AAC Encoding Guide. If audio or video stream is longer, add the -shortest option so that ffmpeg will stop encoding once one file ends. The -map option replaces an audio stream in the input video. ffmpeg only uses the first video stream from the first input and the first audio stream from the second input for the output file. > ffmpeg -ss 00:00:10 -t 5 -i "video.mp4" -ss 0:00:01 -t 5 -i "music.m4a" -map 0:v:0 -map 1:a:0 -y out.mp4 To cut either stream, use -ss [hh:mm:ss] -t [ss] before each of the -i "file.ext": -ss [...] starting point to cut, -t [...] will define the length of the segment in seconds Cutting a video with "-c copy" is very fast. However, if the cut does not coincide with an i-frame, the video is distorted (if -ss is before -i) or blank (if -ss is after -i) until an i-frame in 5-10 secs. Audio is fine. With transcoding, there is no problem with video also, To cut several clips of a video into one, use filters: > ffmpeg -i input.mp4 -vf \ "select='between(t,840,900)+between(t,1650,1870)+between(t,3030,3072)',\ setpts=N/FRAME_RATE/TB" -af \ "aselect='between(t,840,900)+between(t,1650,1870)+between(t,3030,3072)',\ asetpts=N/SR/TB" output.mp4 The arguments to between() give the start and end of each clip, in seconds from the start of the video. Get stats of the video and audio streams: > ffmpeg -report -i LS_01_Highres.mp4 2.4. Concatenating Files ------------------------ Concatenating video files: 1. Create an input file flist.txt: file first.mp4 file second.mp4 ffmpeg -f concat -safe 0 -i flist.txt -c copy output.mp4 2. For .ts (MPEG2) inputs, the following works, but not for .mp4 inputs: > ffmpeg -i "concat:first.ts|second.ts" output.mp4 See details at https://trac.ffmpeg.org/wiki/Concatenate Similarly, to concatenate audio files: 1. Create an input file flist.txt: file first.aac file second.aac ffmpeg -f concat -safe 0 -i flist.txt -c copy output.mp3 2. For .wav or .aac inputs, the following works, but not for .mp3 inputs: > ffmpeg -i "concat:first.wav|second.wav" output.mp3 2.5 Editing Audio ----------------- To scale the volume of the audio stream: > ffmpeg -i input.wav -filter:a "volume=1.5" output.wav Use +/- dB to increase/reduce: > ffmpeg -i input.wav -filter:a "volume=-5dB" output.wav More at: https://trac.ffmpeg.org/wiki/AudioVolume 3. Screen recording on MacBook ------------------------------ 9m22s of screen without any activity, audio off: 2560x1600, 60 fps .mov: 722 MB Convert to .mp4 with ffmpeg: 78 MB ... 1280x800, 15 fps: 26 MB ... -crf 40: 20 MB ... -crf 40, 1440x900, 15fps: 3.2 MB ... -crf 40, 1440x900, 5fps: 1.5 MB > ffmpeg -i ScreenRecord.mov -s 1440x900 -r 15 -crf 40 SR_1440_15_c40.mp4 Reduce size of audio channel: > ffmpeg -i ScreenRecord.mov -s 1440x900 -r 15 -crf 40 -ar 8000 SR_1440_15_c40_a8.mp4 3.1. Self-video using iPad -------------------------- Using back-facing camera, record either with screen-recording, or video in the camera app. 14 sec portrait speaking with little movement ==> 38 MB. Downsampled to 5 fps, 640x480, 8 kHz audio ==> 155 KB (11 KB/sec). No significant difference when the two videos are played fullscreen. > ffmpeg -i Self_video.MP4 -s 480x640 -r 5 -crf 40 -ar 8000 SV_05.mp4