|
| 1 | +project_path: /web/_project.yaml |
| 2 | +book_path: /web/resources/_book.yaml |
| 3 | +description: If your video is short and you want it to auto-play—basically, if you are considering a GIF—then this guide might be just what you need. |
| 4 | + |
| 5 | +{# wf_updated_on: 2016-10-12 #} |
| 6 | +{# wf_published_on: 2016-10-12 #} |
| 7 | + |
| 8 | +# Including Videos {: .page-title } |
| 9 | + |
| 10 | +{% include "web/_shared/contributors/surma.html" %} |
| 11 | + |
| 12 | +If you want to include a video in your article, you should consider uploading it |
| 13 | +to [**YouTube**](https://youtube.com). YouTube will automatically adapt to the |
| 14 | +user’s available bandwidth and offers a familiar UI. However, if your video is |
| 15 | +short and you want it to auto-play—basically if you are considering a |
| 16 | +GIF—then this guide might be just what you need. |
| 17 | + |
| 18 | +## Video source |
| 19 | + |
| 20 | +Your source video should be **high quality** and have a **high frames per |
| 21 | +second**. If you are recording a screencast, make sure you set the bitrate high |
| 22 | +enough to avoid artifacts in the source material. (For a retina screen I usually |
| 23 | +go to ~6Mbit/s). I recommend always recording **at least 60 fps**, _especially_ |
| 24 | +when recording effects, transitions or any kind of animation. |
| 25 | + |
| 26 | +## Converting to web formats |
| 27 | + |
| 28 | +I am using [ffmpeg](https://www.ffmpeg.org/) to convert videos. If you are on a |
| 29 | +Mac with [Homebrew](http://brew.sh/) set up, you can install ffmpeg with: |
| 30 | + |
| 31 | + brew install --with-{libvpx,x265,openh264} ffmpeg |
| 32 | + |
| 33 | +To make sure the videos can be played by both Safari mobile and other browsers, |
| 34 | +we are going to create two versions of the video. One version will be in an |
| 35 | +MPEG4 container (`.mp4`) using the h264 codec. The other version will be in a |
| 36 | +WebM container (`.webm`) using the VP8 codec. You can use the following script |
| 37 | +to automate the process: |
| 38 | + |
| 39 | + #!/bin/bash |
| 40 | + BITRATE=${BITRATE:-500k} |
| 41 | + |
| 42 | + ffmpeg -i $1 ${@:2} -c:v libx264 -b:v ${BITRATE} -movflags +faststart -pix_fmt yuv420p -c:a null ${1%.*}_x264.mp4 |
| 43 | + ffmpeg -i $1 ${@:2} -c:v libvpx -b:v ${BITRATE} -c:a null ${1%.*}_vp8.webm |
| 44 | + |
| 45 | +Notes about the script: |
| 46 | + |
| 47 | +* Usage: `BITRATE=500k ./transcode_video.sh <filename> [additional ffmpeg options]` |
| 48 | +* The script _will_ strip out any audio. |
| 49 | +* You might want to play around with the bitrate for each format individually. |
| 50 | + VP8 tends to achieve the same quality with a smaller bitrate (and therefore |
| 51 | + smaller filesize) than h264. |
| 52 | + |
| 53 | +## Useful options |
| 54 | + |
| 55 | +* `-r 15`: Resample to 15 fps. I use this when I am screencasting the DevTool |
| 56 | + console or a terminal. |
| 57 | +* `-vf 'scale=trunc(iw/4)*2:-2'`: Makes the video half as big. When using it |
| 58 | + make sure both width and height are a multiple of 2. (This is a requirement |
| 59 | + for h264). |
| 60 | +* `-vf 'scale=trunc(iw/8)*4:-2'`: Does the same thing as the previous option, |
| 61 | + except that it makes the video a quarter as big rather than half as big. |
| 62 | + |
| 63 | +I’d recommend NOT using the original resolution for the video, as I expect no |
| 64 | +one to open the video on desktop in fullscreen. A width of roughly under 1000px |
| 65 | +seems like a good choice. |
| 66 | + |
| 67 | +## Hosting |
| 68 | + |
| 69 | +Video assets are not stored in the repository. Any kind of external hosting |
| 70 | +should be fine (like GCS or S3). Googlers can have access to a GCS bucket for |
| 71 | +WebFundamentals assets. |
| 72 | + |
| 73 | +## Including |
| 74 | + |
| 75 | +Include the video in your article using the following markup. The `autoplay` |
| 76 | +attribute can be removed if autoplay is not desired. I recommend keeping |
| 77 | +`controls` in for as long as iOS 9 is around, as it ignores `autoplay` and the |
| 78 | +user needs a way to start the video. I’d also recommend to [add a poster |
| 79 | +image](/web/fundamentals/design-and-ui/media/video#include_a_poster_image), |
| 80 | +especially when autoplay is not enabled. |
| 81 | + |
| 82 | + |
| 83 | + <video controls autoplay loop muted poster="[url to poster image]"> |
| 84 | + <source src="[url to .webm file]" type="video/webm; codecs=vp8"> |
| 85 | + <source src="[url to .mp4 file]" type="video/mp4; codecs=h264"> |
| 86 | + </video> |
| 87 | + |
0 commit comments