Skip to content

Commit 04e8d51

Browse files
committed
add joining video files tutorial
1 parent 49b5302 commit 04e8d51

File tree

6 files changed

+69
-0
lines changed

6 files changed

+69
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
9696
- [How to Create a Watchdog in Python](https://www.thepythoncode.com/article/create-a-watchdog-in-python). ([code](general/directory-watcher))
9797
- [How to Extract Audio from Video in Python](https://www.thepythoncode.com/article/extract-audio-from-video-in-python). ([code](general/video-to-audio-converter))
9898
- [How to Combine a Static Image with Audio in Python](https://www.thepythoncode.com/article/add-static-image-to-audio-in-python). ([code](python-for-multimedia/add-photo-to-audio))
99+
- [How to Concatenate Video Files in Python](https://www.thepythoncode.com/article/concatenate-video-files-in-python). ([code](python-for-multimedia/combine-video))
99100

100101

101102
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)

Diff for: python-for-multimedia/combine-video/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [How to Concatenate Video Files in Python](https://www.thepythoncode.com/article/concatenate-video-files-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
$ python concatenate_video.py --help
7+
```
8+
**Output**:
9+
```
10+
usage: concatenate_video.py [-h] [-c CLIPS [CLIPS ...]] [-r REDUCE] [-o OUTPUT]
11+
12+
Simple Video Concatenation script in Python with MoviePy Library
13+
14+
optional arguments:
15+
-h, --help show this help message and exit
16+
-c CLIPS [CLIPS ...], --clips CLIPS [CLIPS ...]
17+
List of audio or video clip paths
18+
-r REDUCE, --reduce REDUCE
19+
Whether to use the `reduce` method to reduce to the lowest quality on the resulting clip
20+
-o OUTPUT, --output OUTPUT
21+
Output file name
22+
```
23+
- To combine `zoo.mp4` and `directed-by-robert.mp4` to produce `output.mp4`:
24+
```
25+
$ python concatenate_video.py -c zoo.mp4 directed-by-robert.mp4 -o output.mp4
26+
```
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from moviepy.editor import concatenate_videoclips, VideoFileClip
2+
3+
4+
def concatenate(video_clip_paths, output_path, method="compose"):
5+
"""Concatenates several video files into one video file
6+
and save it to `output_path`. Note that extension (mp4, etc.) must be added to `output_path`
7+
`method` can be either 'compose' or 'reduce':
8+
`reduce`: Reduce the quality of the video to the lowest quality on the list of `video_clip_paths`.
9+
`compose`: type help(concatenate_videoclips) for the info"""
10+
# create VideoFileClip object for each video file
11+
clips = [VideoFileClip(c) for c in video_clip_paths]
12+
if method == "reduce":
13+
# calculate minimum width & height across all clips
14+
min_height = min([c.h for c in clips])
15+
min_width = min([c.w for c in clips])
16+
# resize the videos to the minimum
17+
clips = [c.resize(newsize=(min_width, min_height)) for c in clips]
18+
# concatenate the final video
19+
final_clip = concatenate_videoclips(clips)
20+
elif method == "compose":
21+
# concatenate the final video with the compose method provided by moviepy
22+
final_clip = concatenate_videoclips(clips, method="compose")
23+
# write the output video file
24+
final_clip.write_videofile(output_path)
25+
26+
27+
if __name__ == "__main__":
28+
import argparse
29+
parser = argparse.ArgumentParser(
30+
description="Simple Video Concatenation script in Python with MoviePy Library")
31+
parser.add_argument("-c", "--clips", nargs="+",
32+
help="List of audio or video clip paths")
33+
parser.add_argument("-r", "--reduce", action="store_true",
34+
help="Whether to use the `reduce` method to reduce to the lowest quality on the resulting clip")
35+
parser.add_argument("-o", "--output", help="Output file name")
36+
args = parser.parse_args()
37+
clips = args.clips
38+
output_path = args.output
39+
reduce = args.reduce
40+
method = "reduce" if reduce else "compose"
41+
concatenate(clips, output_path, method)
687 KB
Binary file not shown.

Diff for: python-for-multimedia/combine-video/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
moviepy

Diff for: python-for-multimedia/combine-video/zoo.mp4

719 KB
Binary file not shown.

0 commit comments

Comments
 (0)