Record the video screen and join mp4 with ffmpeg

To use this script you need to install ffmpeg in order to join more mp4 files. Many things have to be adapted to the specific characteristic of your computer.

The code to join the mp4

This script can be used to record videos in more than one take and finally join them into one only video output.

This is the script that looks into the folder where the script is and joins all the mp4 together. If you want to use it without placing this script in the folder, go in the site-packages folder of the python installation and save a .pth file where you write the path to this script (ex: c:\pymodules). Once you’ve done this, you can call the script from any folder with python -m joinmp4 if you call this script joinmp4.py, for example. Read this post if you want to know more about this way to call modules.

import os
import glob
 
def concatenate():
    "use ffmpeg to join videos"
    # Starting the ffmpeg command
    stringa = "ffmpeg -i \"concat:"
    # getting the list of mp4 files in the directory
    elenco_video = glob.glob("*.mp4")
    elenco_file_temp = []
    # making the commands for the temporaryfiles to join in .ts format
    for f in elenco_video:
        file = "temp" + str(elenco_video.index(f) + 1) + ".ts"
        os.system("ffmpeg -i " + f + " -c copy -bsf:v h264_mp4toannexb -f mpegts " + file)
        elenco_file_temp.append(file)
    print(elenco_file_temp)
    # now it concatenates the temporayfiles
    for f in elenco_file_temp:
        stringa += f
        if elenco_file_temp.index(f) != len(elenco_file_temp)-1:
            stringa += "|"
        # this goes to the end
        else:
            stringa += "\" -c copy  -bsf:a aac_adtstoasc output.mp4"
    print(stringa)
    os.system(stringa)

concatenate()

Now let’s go to the main program to record the screen (or … record a specific window only).

How to record the screen

import os
import glob

x = 0

def ask():
    fld = "H:\\ffmpeg\\output\\"
    new_fld = input("Nuova cartella (opzionale, invio=stessa cartella)?: ")
    # aggiunge la cartella al percorso
    if new_fld != "":
        fld = fld + new_fld + "\\"
        # crea la cartella, se non c'è già
        if new_fld not in os.listdir("H:\\ffmpeg\\output\\"):
            os.mkdir(fld)
    filename = input("Nome del file: ")
    filename = fld + filename + ".mp4"
    return filename, fld

def automatic_name():
    "Crea il nome da solo / ora chiedo il nome del file prima"
    global x

    fld = "H:\\ffmpeg\\output\\output"
    if not fld + str(x) + ".mp4" in glob.glob(fld + "*.mp4"):
        filename = fld + str(x) + ".mp4"
    else:
        x += 1
        record()
    filename = fld + filename + ".mp4"
    return filename


def record(filename):
    audio = "Microfono (Logitech USB Headset)"
    video_size = "1366x768"
    i = f"-i audio=\"{audio}\""
    screen = "gdigrab"
    compressor = "-af acompressor=threshold=0.089:ratio=9:attack=200:release=1000"

    os.system(f"""ffmpeg -y -rtbufsize 200M -f gdigrab -thread_queue_size 1024 -probesize 10M -r 10 -draw_mouse 1 -video_size {video_size} -i desktop -f dshow -channel_layout stereo -thread_queue_size 1024 {i} {compressor} -c:v libx264 -r 10 -preset ultrafast -tune zerolatency -crf 25 -pix_fmt yuv420p -c:a aac -strict -2 -ac 2 -b:a 128k -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" "{filename}" """)

# filename = "rtmp://youtube_stream_url/03r8-71q2-yrvm-bbe7"
filename, fld = ask()
os.system("start " + fld)
command = "py -m joinmp4"
with open(fld + "join.bat", "w") as file:
    file.write(command)

record(filename)

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.