| 125 | | === Scripting your command line parameters === |
| 126 | | If building complex filtergraphs the command line can get very messy so it can help to break things down into manageable pieces. However one needs to be careful when joining them all together to avoid issues due to your shell and escaped characters. |
| 127 | | |
| 128 | | The following example shows a sample bash script containing a filtergraph of one chain with three filters; yadif, scale and drawtext. |
| | 125 | |
| | 126 | === Synthetic Input === |
| | 127 | |
| | 128 | The [http://ffmpeg.org/ffmpeg-filters.html#color_002c-haldclutsrc_002c-nullsrc_002c-rgbtestsrc_002c-smptebars_002c-smptehdbars_002c-testsrc testsrc source filter] generates a test video pattern showing a color pattern, a scrolling gradient, and a timestamp. This is useful for testing purposes. |
| | 129 | |
| | 130 | This example will create a 10 second output, 30 fps (300 frames total), with a frame size of 1280x720: |
| | 131 | |
| | 132 | {{{ |
| | 133 | ffmpeg -f lavfi -i testsrc=duration=10:size=1280x720:rate=30 output.mpg |
| | 134 | }}} |
| | 135 | |
| | 136 | `ffplay` can also be used to view the resulting filtergraph: |
| | 137 | |
| | 138 | {{{ |
| | 139 | ffplay -f lavfi -i "testsrc=duration=10:size=1280x720:rate=30" |
| | 140 | }}} |
| | 141 | |
| | 142 | You can also specify testsrc as a filter: |
| | 143 | {{{ |
| | 144 | ffmpeg -filter_complex testsrc OUTPUT |
| | 145 | }}} |
| | 146 | |
| | 147 | Another type of testsrc is using the [http://ffmpeg.org/ffmpeg-filters.html#color_002c-haldclutsrc_002c-nullsrc_002c-rgbtestsrc_002c-smptebars_002c-smptehdbars_002c-testsrc smptebars source filter]: |
| | 148 | |
| | 149 | {{{ |
| | 150 | ffmpeg -f lavfi -i "smptebars=duration=5:size=1280x720:rate=30" output.mp4 |
| | 151 | }}} |
| | 152 | |
| | 153 | Or a color |
| | 154 | {{{ |
| | 155 | ./ffmpeg -f lavfi -i color=c=red:size=100x100 |
| | 156 | }}} |
| | 157 | |
| | 158 | There are other options for generating synthetic video input, see [http://stackoverflow.com/questions/11640458/how-can-i-generate-a-video-file-directly-from-an-ffmpeg-filter-with-no-actual-in here] and [http://stackoverflow.com/a/15795112/32453 here] ("generic equation" filter). |
| | 159 | |
| | 160 | == Scripting Filters == |
| | 161 | |
| | 162 | === From a File === |
| | 163 | |
| | 164 | Suppose you want to change some filter parameters based on an input file. Some filters support the option to receive commands via [https://ffmpeg.org/ffmpeg-filters.html#sendcmd_002c-asendcmd `sendcmd`]. Run `ffmpeg -filters` and check the `C` column – if it is present, a filter supports receiving input this way. |
| | 165 | |
| | 166 | For example, if you want to rotate the input at seconds 0, 1, and 2, create a file called `cmd.txt` with the content: |
| | 167 | |
| | 168 | {{{ |
| | 169 | 0 rotate angle '45*PI/180'; |
| | 170 | 1 rotate angle '90*PI/180'; |
| | 171 | 2 rotate angle '180*PI/180'; |
| | 172 | }}} |
| | 173 | |
| | 174 | Now run an example stream: |
| | 175 | |
| | 176 | {{{ |
| | 177 | ffmpeg -f lavfi -i testsrc -filter_complex "[0:v]sendcmd=f=cmd.txt,rotate" -f matroska - | ffplay - |
| | 178 | }}} |
| | 179 | |
| | 180 | Some examples of this technique can be seen [https://stackoverflow.com/a/49600924/435093 here]. |
| | 181 | |
| | 182 | === With a Shell Script === |
| | 183 | |
| | 184 | When building complex filtergraphs, it may help to break the command down into manageable pieces. However one needs to be careful when joining them all together, in order to avoid issues due escaped characters. |
| | 185 | |
| | 186 | The following example shows a sample bash script containing a filtergraph of one chain with three filters: yadif, scale and drawtext. |
| | 187 | |
| 150 | | Note that the filtergraph spans more than one line. The echo command shows the full command as it is executed. Useful for debugging. |
| 151 | | |
| 152 | | The array in the $command_line variable helps avoid loss of the quotes which occurs otherwise. Other shells may behave differently. |
| 153 | | |
| 154 | | |
| 155 | | === Synthetic Input === |
| 156 | | |
| 157 | | The [http://ffmpeg.org/ffmpeg-filters.html#color_002c-haldclutsrc_002c-nullsrc_002c-rgbtestsrc_002c-smptebars_002c-smptehdbars_002c-testsrc testsrc source filter] generates a test video pattern showing a color pattern, a scrolling gradient, and a timestamp. This is useful for testing purposes. |
| 158 | | |
| 159 | | This example will create a 10 second output, 30 fps (300 frames total), with a frame size of 1280x720: |
| 160 | | |
| 161 | | {{{ |
| 162 | | ffmpeg -f lavfi -i testsrc=duration=10:size=1280x720:rate=30 output.mpg |
| 163 | | }}} |
| 164 | | |
| 165 | | `ffplay` can also be used to view the resulting filtergraph: |
| 166 | | |
| 167 | | {{{ |
| 168 | | ffplay -f lavfi -i "testsrc=duration=10:size=1280x720:rate=30" |
| 169 | | }}} |
| 170 | | |
| 171 | | You can also specify testsrc as a filter: |
| 172 | | {{{ |
| 173 | | ffmpeg -filter_complex testsrc OUTPUT |
| 174 | | }}} |
| 175 | | |
| 176 | | Another type of testsrc is using the [http://ffmpeg.org/ffmpeg-filters.html#color_002c-haldclutsrc_002c-nullsrc_002c-rgbtestsrc_002c-smptebars_002c-smptehdbars_002c-testsrc smptebars source filter]: |
| 177 | | |
| 178 | | {{{ |
| 179 | | ffmpeg -f lavfi -i "smptebars=duration=5:size=1280x720:rate=30" output.mp4 |
| 180 | | }}} |
| 181 | | |
| 182 | | Or a color |
| 183 | | {{{ |
| 184 | | ./ffmpeg -f lavfi -i color=c=red:size=100x100 |
| 185 | | }}} |
| 186 | | |
| 187 | | There are other options for generating synthetic video input, see [http://stackoverflow.com/questions/11640458/how-can-i-generate-a-video-file-directly-from-an-ffmpeg-filter-with-no-actual-in here] and [http://stackoverflow.com/a/15795112/32453 here] ("generic equation" filter). |
| 188 | | |
| 189 | | == Scripting Filters from a File == |
| 190 | | |
| 191 | | Suppose you want to change some filter parameters based on an input file. Some filters support the option to receive commands via [https://ffmpeg.org/ffmpeg-filters.html#sendcmd_002c-asendcmd `sendcmd`]. Run `ffmpeg -filters` and check the `C` column – if it is present, a filter supports receiving input this way. |
| 192 | | |
| 193 | | For example, if you want to rotate the input at seconds 0, 1, and 2, create a file called `cmd.txt` with the content: |
| 194 | | |
| 195 | | {{{ |
| 196 | | 0 rotate angle '45*PI/180'; |
| 197 | | 1 rotate angle '90*PI/180'; |
| 198 | | 2 rotate angle '180*PI/180'; |
| 199 | | }}} |
| 200 | | |
| 201 | | Now run an example stream: |
| 202 | | |
| 203 | | {{{ |
| 204 | | ffmpeg -f lavfi -i testsrc -filter_complex "[0:v]sendcmd=f=cmd.txt,rotate" -f matroska - | ffplay - |
| 205 | | }}} |
| 206 | | |
| 207 | | Some examples of this technique can be seen [https://stackoverflow.com/a/49600924/435093 here]. |
| | 209 | |
| | 210 | Note that the filtergraph spans more than one line. The echo command shows the full command as it is executed. This is useful for debugging. |
| | 211 | |
| | 212 | The array in the `$command_line` variable helps avoid loss of the quotes which occurs otherwise. Other shells may behave differently. |