Insert text using FFMPEG, but only in a video snippet

Asked

Viewed 292 times

2

I use the code below to generate new videos (from the respective folder) by inserting the name and CPF of the student in each video.

However, this way, the compilation takes a long time, since the compilation time is the time of the video. I need something faster, I want to insert the text only from the 5th to the 10th second of the video and then stop.

As if dividing the video, insert the text. No need to compile the whole video. I believe it is necessary, before, to make a copy of the videos and then a code that just inserts the text in time.

The script I use is:

#!/bin/bash

echo "Nome do aluno"
read nome;
echo "CPF do aluno"
read cpf;

mkdir $cpf;

echo “iniciando a compilação para o aluno $nome”

[ "$1" ] && cd "$1"

ls -1 *.mp4
[ "$?" -ne 0 ] && echo 'Sem arquivos mp4 nesse diretório' && exit 0
for ARQUIVO in $(ls -1 *.mp4)
do
    ARQ_DESTINO="${ARQUIVO%%.mp4}.mp4"
    echo "Convertendo $ARQUIVO para $ARQ_DESTINO"
    ffmpeg -i "$ARQUIVO" -strict experimental -vf "drawtext=fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf':text='Liberado para $nome - $cpf':x=200:y=10:fontsize=16:enable='between(t,5,10)'" /var/www/Arquivos/videos/$cpf/"$ARQ_DESTINO"
done

As I will sell videos online, I have set up a server to create these videos. However, each class package is around 6 hours long. If 4 people buy per day my server will work all day to mount these new videos. So it becomes unviable, there is a faster solution?

  • I can’t test it, but I believe the solution is split and Concat: Join mp4 files in linux. A file with the first 5 seconds, another with the custom 5 seconds created on-the-fly and another with the rest.

1 answer

-1

See if this solution suits you. I created some temporary files to put the text in just one part.

#!/bin/bash

echo "Nome do aluno"
read nome;
echo "CPF do aluno"
read cpf;

mkdir $cpf;

echo “iniciando a compilação para o aluno $nome”

[ "$1" ] && cd "$1"

ls -1 *.mp4
[ "$?" -ne 0 ] && echo 'Sem arquivos mp4 nesse diretório' && exit 0
for ARQUIVO in $(ls -1 *.mp4)
do
    ARQ_DESTINO="${ARQUIVO%%.mp4}.mp4"
    echo "Convertendo $ARQUIVO para $ARQ_DESTINO"

    # Arquivo s temporários do início meio e fim do vídeo
    ffmpeg -i "$ARQUIVO" -c copy -ss 00:00:00 -to 00:00:05 -avoid_negative_ts 1  inicio.mp4
    ffmpeg -i "$ARQUIVO" -c copy -ss 00:00:05 -to 00:00:10 -avoid_negative_ts 1  meio.mp4
    ffmpeg -i "$ARQUIVO" -c copy -ss 00:00:10  -avoid_negative_ts 1  fim.mp4

    # Inserindo texto na parte do meio
    ffmpeg -i meio.mp4 -vf "drawtext=fontfile='/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf':text='Liberado para $nome - $cpf':x=200:y=10:fontsize=16" -avoid_negative_ts 1   meio-texto.mp4

    # Montando lista para concatenar os arquivos
    echo -e "file 'inicio.mp4'\nfile 'meio-texto.mp4'\nfile 'fim.mp4'" > lista.txt

    # Concatenando os arquivos
    ffmpeg -f concat -i lista.txt -c copy  "/var/www/Arquivos/videos/$cpf/$ARQ_DESTINO"

    # Removendo arquivos temporários utilizados
    rm inicio.mp4 meio.mp4 fim.mp4 meio-texto.mp4 lista.txt

done
  • Murilo, welcome to Stack Overflow in English! I recommend you explain your answer a little, so that it is clear to the user who asked, what was wrong or what modified in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.