Downloading partial YouTube videos with ffmpeg

I spoke at WordCamp US 2023, and wanted to grab a copy of the video of my talk. I always try to keep my own copies of these because I've seen some conferences eventually take these offline in the past.

My presentation was livestreamed through YouTube, but the resulting livestream video is 9.5 hours long!

Normally I would download the full video and trim it locally, but I was on conference WiFi and didn't want to wait for the whole file - so I decided to see if it was possible to download just a subset.

I found this recipe by godofgrunts on StackExchange which looked like what I wanted:

#Arguments: URL, Time stamp -5 seconds, length of clip, video file name

readarray -t urls <<< "$(yt-dlp --youtube-skip-dash-manifest -g "$1")"
ffmpeg -ss $2 -i "${urls[0]}" -ss $2 -i "${urls[1]}" -ss 5 -map 0:v -map 1:a -c:v libx264 -c:a aac -t $3 $4

But when I ran it I got an error, because readarray wasn't available on my Mac.

So I pasted it into GPT-4 with the prompt:

Port this to macOS - I do not have the readarray command

It gave me back this:

# Arguments: URL, Time stamp -5 seconds, length of clip, video file name

IFS=$'\n' urls=($(yt-dlp --youtube-skip-dash-manifest -g "$1"))
ffmpeg -ss $2 -i "${urls[0]}" -ss $2 -i "${urls[1]}" -ss 5 -map 0:v -map 1:a -c:v libx264 -c:a aac -t $3 $4
  1. The IFS variable is set to a newline character, ensuring that the URLs returned by yt-dlp are split on new lines.
  2. The urls array is populated using command substitution and array assignment.
  3. The rest of the script remains unchanged.

I saved that as and ran chmod 755 and... it worked!

I called it like this:

./ '' 8:18:25 3600 simon-wordcamp.mp4

My section of the video starts at around 8h 18m and 25s. I specified 3600 to grab on hour of content (the Q&A went a bit over that, I should have specified more).

It took nearly a full hour for the script to run! It may have been faster to download the whole file and cut it up after all.

But... the result was a 308MB simon-wordcamp.mp4 file.

How that all works

Breaking this down a bit. The first part is this:

yt-dlp --youtube-skip-dash-manifest -g "$1"

Running that to see the output:

yt-dlp --youtube-skip-dash-manifest -g ''


I dug around a bit and it turned out --youtube-skip-dash-manifest is no longer necessary. So this is really:

yt-dlp -g ''

The -g option causes yt-dlp to produce the URL to the streams rather than downloading the video.

It returns two URLs because the first one is the video stream and the secand is the audio stream. We can reformat those URLs to spot that more clearly - the clue is the &mime=video%2Fmp4 in the first compared to &mime=audio%2Fwebm in the second.

Then it calls ffmpeg. Here's a tidied version of that call:

ffmpeg \
  -ss '8:18:25' -i "${VIDEO_STREAM_URL}" \
  -ss '8:18:25' -i "${AUDIO_STREAM_URL}" \
  -ss 5 \
  -map 0:v \
  -map 1:a \
  -c:v libx264 \
  -c:a aac \
  -t 3600 simon-wordcamp.mp4

The two -ss lines seek the video stream and the audio stream to the 8:18:25 mark.

-ss 5 seeks 5 seconds into the combined output. The instructions on StackExchange said to start at least 5s before the section of the video that you want to capture. The StackExchange recipe says this is for "giving me a few seconds to catch a good keyframe".

-map 0:v and -map 1:a sets the first stream up as video and the second stream as audio.

-c:v libx264 and -c:a aac configure the output codecs for video (H.264 video) and audio (AAC).

-t 3600 simon-wordcamp.mp4 sets the duration to an hour (60 * 60 seconds) and specifies the output file.

Created 2023-08-26T11:52:14-04:00, updated 2023-10-27T13:51:22-07:00 · History · Edit