FFmpeg scripts
Process
Each function is first processed by the @process
decorator.
-
It checks if the file at input_path can be accessed and read.
-
If needed according config, it can rename the input file and change the output to avoid using special symbols ( although it may not be necessary since we can use quotes around file names).
-
Then it checks if we rewrite
input_path
since it is not possible to do directly with ffmpeg. If so, it renames the input path as 'fn_tmp' and deletes it after function execution.
Function
-
It makes the plural-form arguments equal in length (see below).
-
It constructs a command line to be run by subprocess.
-
If the length of the command line exceeds 8000, the filter part of the cmd string is saved to cmd_tmp.txt and run by -filter_complex_script.
Arguments
- Arguments whose names end with 's' (plural form) should get a list. If you can pass a list, pass it. This was done on purpose to avoid rerendering a 2-hour video each time we make a minor change - we pass the full list of needed changes.
-
If the length of the plural-form arguments differs from each other, the function will make them equal in length by adding the last element to each of them.
font_colors = ['red'] font_sizes = [500, 200] # Making them equal font_colors = ['red', 'red'] font_sizes = [500, 200]
-
Path arguments should be passed as strings.
- The argument
size
should contain[width, height]
, andsizes
should contain a list of them. - The argument
x_y_coordinate
should contain[x_coordinate, y_coordinate]
, andx_y_coordinates
should contain a list of them. fonts
should be passed with full path like['C\:\\\\Windows\\\\Fonts\\\\Arial.ttf']
, overwise ffmpeg raise error.colors
should be passed as strings like 'FFFFFF' or as defaults colors like 'white'- You can pass ffmpeg things like "1920, -1', 'red', '(w-text_w)/2' to size, color, x_y_coordinate, respectively. Check ffmpeg documentation *
Code
add_audio_to_video(input_video_path, output_path, input_audio_paths, sound_volumes, start_times, durations=None)
Adds audio tracks to a video using ffmpeg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to the output video file. |
required |
input_audio_paths |
list[str]
|
A list of paths to the input audio files. |
required |
sound_volumes |
list[float]
|
A list of sound volumes for each audio track. |
required |
start_times |
list[float]
|
A list of start times for each audio track in seconds. |
required |
durations |
list[float]
|
A list of durations for each audio track. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output video file. |
Source code in ffmpeg_python_utils\main.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
add_blurred_space_around_video(input_video_path, output_path, goal_size, to_resize_video=True, sigma=20)
Adds blurred space around a video using ffmpeg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to the output video file. |
required |
goal_size |
list
|
The desired size of the output video. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
to_resize_video |
bool
|
Whether or not to resize the input video to fit the output size. |
True
|
sigma |
int
|
The amount of blur to apply to the space around the video. |
20
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output video file. |
Source code in ffmpeg_python_utils\main.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
add_colored_space_around_video(input_video_path, output_path, goal_size, to_resize_video=True, color='black', x_y_coordinate=('(ow-iw)/2', '(oh-ih)/2'))
Adds colored space around a video using ffmpeg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to the output video file. |
required |
goal_size |
list
|
The desired size [w,h] of the output video. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
to_resize_video |
bool
|
Whether or not to resize the input video to fit the output size. |
True
|
color |
str
|
The color of the space around the video. Can be passed as built-in ffmpeg color name (black, red etc.) or as hex string like 'FFFFFF'. |
'black'
|
x_y_coordinate |
list
|
The x and y coordinates of the video within the output space. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
('(ow-iw)/2', '(oh-ih)/2')
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output video file. |
Source code in ffmpeg_python_utils\main.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
|
add_image_to_video(input_video_path, output_path, input_image_paths, x_y_coordinates, start_times, durations, img_goal_sizes, opacities=None, fade_duration=0)
Adds images to a video at specified times and positions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
Path to the input video file. |
required |
output_path |
str
|
Path to the output video file. |
required |
input_image_paths |
list[str]
|
List of paths to the input image files. |
required |
x_y_coordinates |
list[list]
|
List of tuples specifying the x and y coordinates of each image. |
required |
start_times |
list[float]
|
List of start times for each image in seconds. |
required |
durations |
list[float]
|
List of durations for each image in seconds. |
required |
img_goal_sizes |
list
|
List of [w, h] specifying the width and height of each image after resizing. Can be passed as built-in ffmpeg thing like ['iw/2', -1] |
required |
opacities |
list
|
List of opacities for each image. |
None
|
fade_duration |
float
|
Duration of fade in and fade out for each image in seconds. |
0
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Path to the output video file. |
Source code in ffmpeg_python_utils\main.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
add_rectangle_to_video(input_path, output_path, start_times, durations, x_y_coordinates, sizes, rect_colors, opacities)
Adds a rectangle to a video using ffmpeg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to the output video file. |
required |
start_times |
list[float]
|
A list of start times in seconds for each rectangle. |
required |
durations |
list[float]
|
A list of durations for each rectangle. |
required |
x_y_coordinates |
list[list]
|
A list of [x,y] coordinates for each rectangle. Can be passed as built-in ffmpeg thing like 'iw/2' |
required |
sizes |
list[list]
|
A list of sizes [w,h] for each rectangle. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
rect_colors |
list[str]
|
A list of colors for each rectangle. Can be passed as built-in ffmpeg color name (black, red etc.) or as hex string like 'FFFFFF'. |
required |
opacities |
list[float]
|
A list of opacities for each rectangle. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output video file. |
Source code in ffmpeg_python_utils\main.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
add_text_to_video(input_video_path, output_path, texts, fonts_paths, font_sizes, font_colors, start_times, durations, x_y_coordinates, fade_duration=0, border_color='black', border_width=5)
Adds text to a video and saves it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
Path to the video to add texts to. |
required |
output_path |
str
|
Path to the new video. |
required |
texts |
list[str]
|
List of texts to add. |
required |
fonts_paths |
list[str]
|
List of paths to fonts. At windows should be like "C\:\Windows\Fonts\Arial.ttf" |
required |
font_sizes |
list[int]
|
List of font sizes. |
required |
font_colors |
list
|
List of font colors. Can be passed as built-in ffmpeg color name (black, red etc.) or as hex string like 'FFFFFF'. |
required |
start_times |
list[float]
|
List of start times in seconds. |
required |
durations |
list[float]
|
List of durations in seconds. |
required |
x_y_coordinates |
list[list]
|
List of [x, y] coordinates. Can be passed as built-in ffmpeg thing like '(w-text_w)/2' |
required |
fade_duration |
float
|
Duration of the fade effect. |
0
|
border_color |
str
|
Color of the border. Can be passed as built-in ffmpeg color name (black, red etc.) or as hex string like 'FFFFFF'. |
'black'
|
border_width |
int
|
Width of the border. |
5
|
Returns:
Name | Type | Description |
---|---|---|
str |
Path to the new video (output_path). |
Source code in ffmpeg_python_utils\main.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
add_video_to_video(input_video_path, output_path, video_to_overlay_paths, goal_sizes, x_y_coordinates, start_times, durations, opacities=None, fade_duration=0)
Adds a video overlay to a video using ffmpeg.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to the output video file. |
required |
video_to_overlay_paths |
list[str]
|
A list of paths to the video files to overlay. |
required |
goal_sizes |
list[list]
|
A list of sizes [w,h] for each overlay video. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
x_y_coordinates |
list[list]
|
A list of [x,y] coordinates for each overlay video. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
start_times |
list[float]
|
A list of start times for each overlay video in seconds. |
required |
durations |
list[float]
|
A list of durations for each overlay video. |
required |
opacities |
list[float]
|
A list of opacities for each overlay video. |
None
|
fade_duration |
float
|
The duration of the fade in/out effect for each overlay video. |
0
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output video file. |
Source code in ffmpeg_python_utils\main.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
get_audio_from_video(input_video_path, output_path='audio.wav')
Extracts the audio from a video file using ffmpeg and saves it to a WAV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to save the output audio file. |
'audio.wav'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output audio file. |
Source code in ffmpeg_python_utils\main.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
|
get_audio_info(input_audio_file)
Get the duration, bitrate, and sample rate of an audio file using ffprobe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_audio_file |
str
|
The path to the input audio file. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing the duration, bitrate, and sample rate of the audio. |
Source code in ffmpeg_python_utils\main.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
|
get_concantenated_videos(input_video_paths, output_path, effects=None, transition_durations=None)
Concatenates multiple videos together using ffmpeg. If effects is False-like, then no transition_duration implemented.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_paths |
list[str]
|
A list of paths to the input video files. |
required |
output_path |
str
|
The path to the output video file. |
required |
effects |
list[str]
|
A list of transition effects to use between each video. See https://trac.ffmpeg.org/wiki/Xfade for options. |
None
|
transition_durations |
list[float]
|
A list of durations for each transition. |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output video file. |
Source code in ffmpeg_python_utils\main.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
|
get_cropped_video(input_video_path, output_path, size, x_y_coordinate)
Crop a video file to a specified size and position.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to save the cropped video file. |
required |
size |
list
|
A tuple containing the width and height of the cropped video. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
x_y_coordinate |
list
|
A tuple containing the x and y coordinates of the top-left corner of the cropped video. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the cropped video file. |
Source code in ffmpeg_python_utils\main.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
|
get_frame(input_video_path, output_path, time)
Extract a single frame from a video file at a specified time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to save the extracted frame. |
required |
time |
float
|
The time at which to extract the frame in seconds. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the extracted frame. |
Source code in ffmpeg_python_utils\main.py
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
|
get_image_info(input_image_path)
Get the width and height of a video file using ffprobe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_image_path |
str
|
The path to the input video file. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing the width and height of the video. |
Source code in ffmpeg_python_utils\main.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
get_mirrored_video(input_video_path, output_path)
Creates a horizontally mirrored version of a video file using ffmpeg and saves it to a new file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to save the output mirrored video file. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output mirrored video file. |
Source code in ffmpeg_python_utils\main.py
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 |
|
get_resized_image(input_image_path, output_path, size)
Resize an image file to a specified size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_image_path |
str
|
The path to the input image file. |
required |
output_path |
str
|
The path to save the resized image file. |
required |
size |
list
|
A tuple containing the width and height of the resized image. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the resized image file. |
Source code in ffmpeg_python_utils\main.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 |
|
get_resized_video(input_video_path, output_path, size)
Resize a video file to a specified size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to save the resized video file. |
required |
size |
list
|
A tuple containing the width and height of the resized video. Can be passed as built-in ffmpeg thing like ['iw/2', -1] or as [int, int] |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the resized video file. |
Source code in ffmpeg_python_utils\main.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
|
get_rotated_video(input_video_path, output_path, degree)
Rotates a video file clockwise by a specified degree using ffmpeg and saves it to a new file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to save the output rotated video file. |
required |
degree |
float
|
The degree to rotate the video clockwise. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output rotated video file. |
Source code in ffmpeg_python_utils\main.py
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 |
|
get_subclips_with_sound(input_video_path, output_path, subclip_times)
Extract subclips from a video file and concatenate them into a single video file with sound.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
output_path |
str
|
The path to save the concatenated video file. |
required |
subclip_times |
list[list[float]]
|
A list of lists containing the start and end times in seconds of each subclip in the format [[start1, end1], [start2, end2], ...]. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the concatenated video file. |
Source code in ffmpeg_python_utils\main.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
|
get_video_from_picture(input_path, output_path, duration)
Creates a video file from a single image file using ffmpeg and saves it to a new file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_path |
str
|
The path to the input image file. |
required |
output_path |
str
|
The path to save the output video file. |
required |
duration |
float
|
The duration of the output video file in seconds. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The path to the output video file. |
Source code in ffmpeg_python_utils\main.py
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 |
|
get_video_info(input_video_path)
Get the duration, width, height, and FPS of a video file using ffprobe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_video_path |
str
|
The path to the input video file. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing the duration, width, height, and FPS of the video. |
Source code in ffmpeg_python_utils\main.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 |
|