# Tool for splicing asciinema clips together?

**URL:** <https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284>\
**Category:** Help\
**Created:** [September 26, 2019, 5:10pm UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284 "2019-09-26T17:10:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradcray](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.asciinema.org/bradcray/32/526_2.png) [@bradcray](https://discourse.asciinema.org/u/bradcray)\
**Post date:** [September 26, 2019, 5:10pm UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284/1 "2019-09-26T17:10:54Z")

</div>

Hi Asciinema community —

I’m curious whether anyone has a tool or script that can splice asciinema clips together? I understand that I can use the `rec --append` option to extend an existing clip with a new recording. But for the movie I’m trying to make, such a tool would make it easier to create a number of short clips and then to splice them together in arbitrary orders, or to later replace an intermediate clip without having to re-record all the clips that follow it.

Thanks for any pointers,  
-Brad

---

<div class="post-metadata">

**Author:** ![bradcray](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.asciinema.org/bradcray/32/526_2.png) [@bradcray](https://discourse.asciinema.org/u/bradcray)\
**Post date:** [October 10, 2019, 11:52am UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284/2 "2019-10-10T11:52:02Z")

</div>

For anyone curious about this question, I believe my team has now developed such a script. I’ll post once I get more experience with it and permission from the author to do so.

-Brad

---

<div class="post-metadata">

**Author:** ![oldfart](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.asciinema.org/oldfart/32/28_2.png) [@oldfart](https://discourse.asciinema.org/u/oldfart)\
**Post date:** [October 24, 2019, 10:15pm UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284/3 "2019-10-24T22:15:23Z")

</div>

Well, please. We are al ears now that you spoke about it

Couldnt it be asimple as using the append with a play?

something like…

`asciinema rec --append $(asciinema play something.cast)`

---

<div class="post-metadata">

**Author:** ![bradcray](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.asciinema.org/bradcray/32/526_2.png) [@bradcray](https://discourse.asciinema.org/u/bradcray)\
**Post date:** [October 25, 2019, 12:16am UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284/4 "2019-10-25T00:16:46Z")

</div>

> [@oldfart](#):
>
> Couldnt it be asimple as using the append with a play?
> 
> something like…
> 
> `asciinema rec --append $(asciinema play something.cast)`

That’s interesting and clever, and perhaps it would work for me… I’d hoped to give a list of clips to a script and have it splice them all together, but one could probably create such a script using that pattern.

I got distracted from this effort temporarily, so haven’t had the chance to test the draft script as much as I’d like yet to have full confidence in it, but to avoid keeping you in suspense now that I know someone saw the original post, here’s the rough draft:

```auto
#!/usr/bin/env python                                                           

#                                                                               
# Script for splicing asciinema clips together, removing final exits            
# from each.                                                                    
# Authored by Engin Kayraklioglu, October 2019                                  
#                                                                               

import sys
import re

first_line = False
cur_time_off = 0.

in_format = re.compile('\[(\d+\.\d+), "(.*)", "(.*)"\]')
end_markers = ('\\u001b[0;94m$ \\u001b[1;35m',
               '\\u001b[0;94m> \\u001b[1;35m')
out_format = '[{}, "{}", "{}"]\n'.format

def get_triple_from_line(l):
    match = in_format.match(l)
    if match:
        return match.groups(0)
    else:
        print('Unmatched line ', l)

out_file_name = sys.argv[-1]
in_file_names = sys.argv[1:-1]

with open(out_file_name, 'w') as out_file:
    for in_file_name in in_file_names:
        last_script = in_file_name == in_file_names[-1]
        with open(in_file_name) as in_file:
            found_end_marker = False
            for l in in_file:
                if not first_line:
                    out_file.write(l)
                    first_line = True
                    continue
                elif l[0] == '{':
                    continue

                f1, f2, f3 = get_triple_from_line(l)
                if not last_script:
                    if f3 in end_markers:
                        break
                else:
                    if found_end_marker and f3 != 'exit\\r\\n':
                        continue
                    if f3 in end_markers:
                        found_end_marker = True

                read_time = float(f1)
                out_file.write(out_format(read_time+cur_time_off, f2, f3))

            cur_time_off = read_time

```

---

<div class="post-metadata">

**Author:** ![oldfart](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.asciinema.org/oldfart/32/28_2.png) [@oldfart](https://discourse.asciinema.org/u/oldfart)\
**Post date:** [November 13, 2019, 11:59pm UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284/5 "2019-11-13T23:59:20Z")

</div>

It’s a start.

The exit(s) are kept from all files it seems. Do you happen to have another version?

---

<div class="post-metadata">

**Author:** ![bradcray](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.asciinema.org/bradcray/32/526_2.png) [@bradcray](https://discourse.asciinema.org/u/bradcray)\
**Post date:** [November 14, 2019, 12:13am UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284/6 "2019-11-14T00:13:37Z")

</div>

My intermediate exits get removed. I think the difference is due to differences in our prompts. I believe this line indicates the prompts that it’s looking for:

```auto
end_markers = ('\\u001b[0;94m$ \\u001b[1;35m',
               '\\u001b[0;94m> \\u001b[1;35m')

```

so you probably would need to update it to reflect your prompt format (?).

---

<div class="post-metadata">

**Author:** ![ku1ik](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.asciinema.org/ku1ik/32/171_2.png) [@ku1ik](https://discourse.asciinema.org/u/ku1ik)\
**Post date:** [April 20, 2024, 3:35pm UTC](https://discourse.asciinema.org/t/tool-for-splicing-asciinema-clips-together/284/7 "2024-04-20T15:35:41Z")

</div>

If anyone is still looking for a way to combine multiple casts together then the upcoming asciinema 3.0 has built-in support for that:

```sh
asciinema cat clip1.cast clip2.cast clip3.cast >final.cast

```

3.0 is not released yet but there’s release candidate build you can get from here if you want to play with it:

> **[Release v3.0.0-rc.1 · asciinema/asciinema](https://github.com/asciinema/asciinema/releases/tag/v3.0.0-rc.1)**
>
> This is the first release candidate version of the brand new asciinema CLI 3.0.
> It's a complete rewrite in Rust, bringing a lot of improvements and long requested features.
> A major release is a goo...
