coveralls: 
[![Coverage Status](https://coveralls.io/builds/30524683/badge)](https://coveralls.io/builds/30524683)

Coverage increased (+0.03%) to 66.174% when pulling **09a365021014b84c50597fa695119fbe1a11b706 on tburrows13:add-set_layer** into **90e42c108dcbefaf3e538d525800db9345fee714 on Zulko:master**.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4010da4..65103a9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - Optional `encoding` parameter in `SubtitlesClip` [#1043]
 - Added new `ffmpeg_stabilize_video()` function in `ffmpeg_tools`
 - Optional `temp_audiofile_path` parameter in `VideoClip.write_videofile()` to specify where the temporary audiofile should be created [#1144]
+- `VideoClip.set_layer()` to specify the layer of the clip for use when creating a `CompositeVideoClip` [#1176]
 - `ffmpeg_parse_infos` additionally returns `"video_bitrate"` and `"audio_bitrate"` values [#930]
 - Access to the source video's bitrate in a `VideoFileClip` or `AudioFileClip` through `videoclip.reader.bitrate` and `audioclip.reader.bitrate` [#930]
 
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index f182eb2..428a8b1 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -80,6 +80,11 @@ class VideoClip(Clip):
     relative_pos
       See variable ``pos``.
 
+    layer
+      Indicates which clip is rendered on top when two clips overlap in
+      a CompositeVideoClip. The highest number is rendered on top.
+      Default is 0.
+
     """
 
     def __init__(
@@ -90,6 +95,7 @@ class VideoClip(Clip):
         self.audio = None
         self.pos = lambda t: (0, 0)
         self.relative_pos = False
+        self.layer = 0
         if make_frame:
             self.make_frame = make_frame
             self.size = self.get_frame(0).shape[:2][::-1]
@@ -778,6 +784,15 @@ class VideoClip(Clip):
         else:
             self.pos = lambda t: pos
 
+    @apply_to_mask
+    @outplace
+    def set_layer(self, layer):
+        """Set the clip's layer in compositions. Clips with a greater ``layer``
+        attribute will be displayed on top of others.
+
+        Note: Only has effect when the clip is used in a CompositeVideoClip."""
+        self.layer = layer
+
     # --------------------------------------------------------------
     # CONVERSIONS TO OTHER TYPES
 
@@ -1325,7 +1340,7 @@ class BitmapClip(VideoClip):
             "B": (0, 0, 255),
             "O": (0, 0, 0),  # "O" represents black
             "W": (255, 255, 255),
-            "A": (89, 225, 62),  # "A", "C", "D" and "E" represent arbitrary colors
+            "A": (89, 225, 62),  # "A", "C", "D", "E", "F" represent arbitrary colors
             "C": (113, 157, 108),
             "D": (215, 182, 143),
             "E": (57, 26, 252),
@@ -1348,6 +1363,7 @@ class BitmapClip(VideoClip):
                 "C": (113, 157, 108),
                 "D": (215, 182, 143),
                 "E": (57, 26, 252),
+                "F": (225, 135, 33),
             }
 
         frame_list = []
diff --git a/moviepy/video/compositing/CompositeVideoClip.py b/moviepy/video/compositing/CompositeVideoClip.py
index 6349bef..fc97498 100644
--- a/moviepy/video/compositing/CompositeVideoClip.py
+++ b/moviepy/video/compositing/CompositeVideoClip.py
@@ -20,8 +20,14 @@ class CompositeVideoClip(VideoClip):
       The size (height x width) of the final clip.
 
     clips
-      A list of videoclips. Each clip of the list will
-      be displayed below the clips appearing after it in the list.
+      A list of videoclips.
+
+      Clips with a higher ``layer`` attribute will be dislayed
+      on top of other clips in a lower layer.
+      If two or more clips share the same ``layer``,
+      then the one appearing latest in ``clips`` will be displayed
+      on top (i.e. it has the higher layer).
+
       For each clip:
        
       - The attribute ``pos`` determines where the clip is placed.
@@ -78,6 +84,9 @@ class CompositeVideoClip(VideoClip):
             self.bg = ColorClip(size, color=self.bg_color, ismask=ismask)
             self.created_bg = True
 
+        # order self.clips by layer
+        self.clips = sorted(self.clips, key=lambda clip: clip.layer)
+
         # compute duration
         ends = [c.end for c in self.clips]
         if None not in ends:
@@ -97,6 +106,7 @@ class CompositeVideoClip(VideoClip):
                 .set_position(c.pos)
                 .set_end(c.end)
                 .set_start(c.start, change_end=False)
+                .set_layer(c.layer)
                 for c in self.clips
             ]
 

CompositeVideoClip - Different rules for clip appereance
I have stumbled upon this problem while coding a feature involving clips being sorted in a different manner than the one intended by the CompositeVideoClip.

What i mean by that is that usually while working with any video (outside of Python). You imagine the videos in layers where You would like to have different layers.

At this point every Video is in its own layer based on the index on the list [clip0, transition_clip, clip1], would make it that clip1 is cutting the transition_clip.

I haven't tested any workarounds. but I suspect that this might be solved by using Two CompositeVideoClips with two lists of clips with different .start_time() parameters.

That would mean using:
```
transition_clips = CompositeVideoClip([transition_clip, transition_clip, transition_clip])
legit_clips = CompositeVideoClip([clip0, clip1, clip2])

final_composition = CompositeVideoClip([legit_clips, transition_clips])
```
Which might not be computationally efficient at all as there are 3x nested clips.

Please note that the code shown above is not fully operational. I have ommited .set_start()

Which leads me to thing that this might be solved simply by sorting the lists somehow.

diff --git a/tests/test_VideoClip.py b/tests/test_VideoClip.py
index f8733a1..9ef5051 100644
--- a/tests/test_VideoClip.py
+++ b/tests/test_VideoClip.py
@@ -6,6 +6,7 @@ from numpy import pi, sin, array
 from moviepy.audio.AudioClip import AudioClip
 from moviepy.audio.io.AudioFileClip import AudioFileClip
 from moviepy.utils import close_all_clips
+from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
 from moviepy.video.fx.speedx import speedx
 from moviepy.video.io.VideoFileClip import VideoFileClip
 from moviepy.video.VideoClip import ColorClip, BitmapClip
@@ -191,6 +192,35 @@ def test_setopacity():
     close_all_clips(locals())
 
 
+def test_set_layer():
+    bottom_clip = BitmapClip([["ABC"], ["BCA"], ["CAB"]]).set_fps(1).set_layer(1)
+    top_clip = BitmapClip([["DEF"], ["EFD"]]).set_fps(1).set_layer(2)
+
+    composite_clip = CompositeVideoClip([bottom_clip, top_clip])
+    reversed_composite_clip = CompositeVideoClip([top_clip, bottom_clip])
+
+    # Make sure that the order of clips makes no difference to the composite clip
+    assert composite_clip.subclip(0, 2) == reversed_composite_clip.subclip(0, 2)
+
+    # Make sure that only the 'top' clip is kept
+    assert top_clip.subclip(0, 2) == composite_clip.subclip(0, 2)
+
+    # Make sure that it works even when there is only one clip playing at that time
+    target_clip = BitmapClip([["DEF"], ["EFD"], ["CAB"]]).set_fps(1)
+    assert composite_clip == target_clip
+
+
+def test_compositing_with_same_layers():
+    bottom_clip = BitmapClip([["ABC"], ["BCA"]]).set_fps(1)
+    top_clip = BitmapClip([["DEF"], ["EFD"]]).set_fps(1)
+
+    composite_clip = CompositeVideoClip([bottom_clip, top_clip])
+    reversed_composite_clip = CompositeVideoClip([top_clip, bottom_clip])
+
+    assert composite_clip == top_clip
+    assert reversed_composite_clip == bottom_clip
+
+
 def test_toimageclip():
     clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
     clip = clip.to_ImageClip(t=0.1, duration=0.4)

pip install -e .[optional]
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-xdist pytest-mock pytest-asyncio
apt-get update apt-get install -y gcc
3.7
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
charset-normalizer==3.4.1
coverage==7.2.7
cycler==0.11.0
decorator==4.4.2
exceptiongroup==1.2.2
execnet==2.0.2
idna==3.10
imageio==2.31.2
imageio-ffmpeg==0.5.1
importlib-metadata==6.7.0
iniconfig==2.0.0
joblib==1.3.2
kiwisolver==1.4.5
matplotlib==2.2.5
-e git+https://github.com/Zulko/moviepy.git@3b25a768ba7f68ed8b8fdd266e93f3ac6a89e9a0#egg=moviepy
networkx==2.6.3
numpy==1.21.6
opencv-python==3.4.18.65
packaging==24.0
Pillow==9.5.0
pluggy==1.2.0
proglog==0.1.10
pyparsing==3.1.4
pytest==7.4.4
pytest-asyncio==0.21.2
pytest-cov==4.1.0
pytest-mock==3.11.1
pytest-xdist==3.5.0
python-dateutil==2.9.0.post0
python-dotenv==0.21.1
pytz==2025.2
PyWavelets==1.3.0
requests==2.31.0
scikit-image==0.19.3
scikit-learn==1.0.2
scipy==1.4.1
six==1.17.0
threadpoolctl==3.1.0
tifffile==2021.11.2
tomli==2.0.1
tqdm==4.67.1
typing_extensions==4.7.1
urllib3==2.0.7
youtube-dl==2021.12.17
zipp==3.15.0

name: moviepy
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - charset-normalizer==3.4.1
      - coverage==7.2.7
      - cycler==0.11.0
      - decorator==4.4.2
      - exceptiongroup==1.2.2
      - execnet==2.0.2
      - idna==3.10
      - imageio==2.31.2
      - imageio-ffmpeg==0.5.1
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - joblib==1.3.2
      - kiwisolver==1.4.5
      - matplotlib==2.2.5
      - networkx==2.6.3
      - numpy==1.21.6
      - opencv-python==3.4.18.65
      - packaging==24.0
      - pillow==9.5.0
      - pluggy==1.2.0
      - proglog==0.1.10
      - pyparsing==3.1.4
      - pytest==7.4.4
      - pytest-asyncio==0.21.2
      - pytest-cov==4.1.0
      - pytest-mock==3.11.1
      - pytest-xdist==3.5.0
      - python-dateutil==2.9.0.post0
      - python-dotenv==0.21.1
      - pytz==2025.2
      - pywavelets==1.3.0
      - requests==2.31.0
      - scikit-image==0.19.3
      - scikit-learn==1.0.2
      - scipy==1.4.1
      - six==1.17.0
      - threadpoolctl==3.1.0
      - tifffile==2021.11.2
      - tomli==2.0.1
      - tqdm==4.67.1
      - typing-extensions==4.7.1
      - urllib3==2.0.7
      - youtube-dl==2021.12.17
      - zipp==3.15.0
prefix: /opt/conda/envs/moviepy

tests/test_VideoClip.py::test_set_layer tests/test_VideoClip.py::test_compositing_with_same_layers
tests/test_VideoClip.py::test_write_gif_ImageMagick tests/test_VideoClip.py::test_write_gif_ImageMagick_tmpfiles
tests/test_VideoClip.py::test_aspect_ratio tests/test_VideoClip.py::test_check_codec tests/test_VideoClip.py::test_write_frame_errors tests/test_VideoClip.py::test_write_frame_errors_with_redirected_logs tests/test_VideoClip.py::test_write_videofiles_with_temp_audiofile_path tests/test_VideoClip.py::test_save_frame tests/test_VideoClip.py::test_write_image_sequence tests/test_VideoClip.py::test_write_gif_imageio tests/test_VideoClip.py::test_write_gif_ffmpeg tests/test_VideoClip.py::test_write_gif_ffmpeg_tmpfiles tests/test_VideoClip.py::test_subfx tests/test_VideoClip.py::test_oncolor tests/test_VideoClip.py::test_setaudio tests/test_VideoClip.py::test_setaudio_with_audiofile tests/test_VideoClip.py::test_setopacity tests/test_VideoClip.py::test_toimageclip tests/test_VideoClip.py::test_withoutaudio
moviepy/video/compositing/CompositeVideoClip.py CHANGELOG.md moviepy/video/VideoClip.py
moviepy/video/compositing/CompositeVideoClip.py CHANGELOG.md moviepy/video/VideoClip.py