# ffmpeg: download streams, trim video, concat files

### Download individual stream files

```bash
ffmpeg -i <input-file> -c <codec> <output-file>
```

Start with the `ffmpeg` command

`-i` = input file

`<input-file>` either a stream url or local file

`-c` = codec to be used

`<codec>`\= I always use `copy` to use the same codec as the stream, or you can specify any other codec you want

`<output-file>` = the filename you want to save

### Trim the parts you don't want

```bash
ffmpeg -ss <hh:mm:ss> -i <input-file> -to <hh:mm:ss> -c <codec> <output-file>
```

Start with the `ffmpeg` command

`-ss` = start time of the video to trim

`<hh:mm:ss>` = must be in the format of hours:minutes:seconds

`-i` = input file

`<input-file>` either a stream url or local file

`-to` = total length of clip time, this can be confusing at first because you need to calculate the elapsed time of the clip. e.g. If you want to trim a clip that begins at 01:23:33 (one hour, 23 minutes, 33 seconds) and you want the video trimmed to 01:23:55 (one hour, 23 minutes, 55 seconds), the value of `to` would be `00:00:22` (22 seconds elapsed time)

`-c` = codec to be used

`<codec>`\= I always use `copy` to use the same codec as the stream, or you can specify any other codec you want

`<output-file>` = the filename you want to save, make sure you create a copy and don't overwrite the original file you are trimming

### Save all your files with a consistent naming pattern to easily collect them for future use

```bash
for f in <pattern>; do echo "file '$f'" >> <output-file>; done
```

let's write a bash script to easily create a playlist of the files you want to concat

`for <variable> in` is a bash loop to find all instances of the pattern given

`f` is a variable name for "file", it could be anything

`<pattern>` is a regex pattern of the filename e.g. `*title-mm-dd-yy*`

`do` begins the looping process

`echo "file '$f'"` tells bash to write the text `"file <filename>"` from the variable `f` to stdout (the terminal)

`>>` is a direction operator in Linux to write a file. When two arrows are present, it signifies append to the existing file

`<output-file>` should be a text file e.g. `files.txt`

`<semi-colon>` is used to end the previous command

we need to finish with a `done` command to finish the loop

This will create a `files.txt` with a playlist of the matching files

> file 'game-01-01-23\_1.mp4'
> 
> file 'game-01-01-23\_2.mp4'
> 
> file 'game-01-01-23\_3.mp4'
> 
> file 'game-01-01-23\_4.mp4'

### Concat your files

```bash
ffmpeg -f concat -i <input-file> -safe 0 -c <codec> <output-file>
```

Start with the `ffmpeg` command

`-f`\= indicates to ffmpeg to list each file for concatenation

`concat` = concatenate command

`-i` = input file

`<input-file>` either a stream url or local file

`-safe 0` = dictates unsafe file names should be ignored, this is mainly used to allow relative paths

`-c` = codec to be used

`<codec>`\= I always use `copy` to use the same codec as the stream, or you can specify any other codec you want

`<output-file>` = the filename you want to save, make sure you create a copy and don't overwrite the original files you are using to create the final version
