Cria uma miniatura do vídeo em um período de tempo aleatório

5

Estou usando este script bash para gerar miniaturas de vídeos:

#!/bin/bash
source_dir="."
output_dir="/output"
input_file_types=(avi wmv flv mkv mpg mp4)
output_file_type="jpg"
r1=$(( ( RANDOM % 10 )  + 5 ))

convert() {
        echo "" | ffmpeg -ss 00:"r1":05 -y -i "$1" -an -f image2 -vframes 1 "$output_dir/$2"
}

for input_file_type in "${input_file_types[@]}"
do
        find "$source_dir" -name "*.$input_file_type" -print0 | while IFS= read -r -d $'
mediainfo myvideo.mp4 | grep Duration
Duration                                 : 5mn 7s
Duration                                 : 5mn 7s
Duration                                 : 5mn 7s
' in_file do echo "Processing…" echo ">Input $in_file" # Replace the file type out_file=$(echo "$in_file" | sed "s/\(.*\.\)$input_file_type/$output_file_type/g") # The above can be shortened to # out_file="${in_file%.$input_file_type}.$output_file_type" echo ">Output $out_file" # Convert the file convert "$in_file" "$out_file" if [ $? != 0 ] then echo "$in_file had problems" >> handbrake-errors.log fi echo ">Finished $out_file\n\n" done done echo "DONE CONVERTING FILES"

O que eu quero é calcular o tempo total de vídeo e gerar uma miniatura a partir de uma hora de vídeo aleatória.

#!/bin/bash
source_dir="."
output_dir="/output"
input_file_types=(avi wmv flv mkv mpg mp4)
output_file_type="jpg"
r1=$(( ( RANDOM % 10 )  + 5 ))

convert() {
        echo "" | ffmpeg -ss 00:"r1":05 -y -i "$1" -an -f image2 -vframes 1 "$output_dir/$2"
}

for input_file_type in "${input_file_types[@]}"
do
        find "$source_dir" -name "*.$input_file_type" -print0 | while IFS= read -r -d $'
mediainfo myvideo.mp4 | grep Duration
Duration                                 : 5mn 7s
Duration                                 : 5mn 7s
Duration                                 : 5mn 7s
' in_file do echo "Processing…" echo ">Input $in_file" # Replace the file type out_file=$(echo "$in_file" | sed "s/\(.*\.\)$input_file_type/$output_file_type/g") # The above can be shortened to # out_file="${in_file%.$input_file_type}.$output_file_type" echo ">Output $out_file" # Convert the file convert "$in_file" "$out_file" if [ $? != 0 ] then echo "$in_file had problems" >> handbrake-errors.log fi echo ">Finished $out_file\n\n" done done echo "DONE CONVERTING FILES"

Como faço para integrar grep Duration no script bash acima para que eu possa obter uma miniatura com um tempo aleatório com base no tempo total de vídeo?

    
por juicebyah 13.07.2015 / 19:44

2 respostas

2

Tente pegar a duração em segundos, pois isso tornará tudo mais fácil:

Sua função de conversão pode acabar sendo:

convert() {
    # Get duration in milliseconds, then convert to seconds
    duration=$(($(mediainfo --Inform="General;%Duration%" "${in_file}" ) / 1000 ))

    # Calculate random time
    random_time=$(date -u -d @$(shuf -i 0-${duration} -n 1) +"%T")

    # Take screenshot
    ffmpeg -ss ${random_time} -i "$in_file" -vframes 1 -q:v 2 "$output_dir/$output_file"
}
    
por 13.07.2015 / 19:57
0

este bash fará

#!/bin/bash
source_dir="."
output_dir="."
input_file_types=(avi wmv flv mkv mpg mp4)
output_file_type="jpg"

convert() {
        echo "" | ffmpeg -ss $ss -y -i "$in_file" -an -f image2 -vframes 1 "$output_dir/$out_file"
}

for input_file_types in "${input_file_types[@]}"
do

        find "$source_dir" -name "*.$input_file_types" -print0 | while IFS= read -r -d $'
#!/bin/bash
source_dir="."
output_dir="."
input_file_types=(avi wmv flv mkv mpg mp4)
output_file_type="jpg"

convert() {
        echo "" | ffmpeg -ss $ss -y -i "$in_file" -an -f image2 -vframes 1 "$output_dir/$out_file"
}

for input_file_types in "${input_file_types[@]}"
do

        find "$source_dir" -name "*.$input_file_types" -print0 | while IFS= read -r -d $'%pre%' in_file
        do
                echo "Processing…"
                echo ">Input  "$in_file
                # Replace the file type
                out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_types/$output_file_type/g")
                echo ">Output "$out_file

    # get video duration
    fulltime='ffmpeg -i "$in_file" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//';
    hour='echo $fulltime | cut -d ':' -f 1';
    minute='echo $fulltime | cut -d ':' -f 2';
    second='echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1';

    seconds='expr 3600 \* $hour + 60 \* $minute + $second';
    ss='expr $seconds / 2'; # from the middle of video

    # Convert the file
                convert "$in_file" "$out_file"

                if [ $? != 0 ]
                then
                    echo "$in_file had problems" >> ffmpeg-errors.log
                fi

                echo ">Finished "$out_file "\n\n"
        done
done
' in_file do echo "Processing…" echo ">Input "$in_file # Replace the file type out_file=$(echo $in_file|sed "s/\(.*\.\)$input_file_types/$output_file_type/g") echo ">Output "$out_file # get video duration fulltime='ffmpeg -i "$in_file" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//'; hour='echo $fulltime | cut -d ':' -f 1'; minute='echo $fulltime | cut -d ':' -f 2'; second='echo $fulltime | cut -d ':' -f 3 | cut -d '.' -f 1'; seconds='expr 3600 \* $hour + 60 \* $minute + $second'; ss='expr $seconds / 2'; # from the middle of video # Convert the file convert "$in_file" "$out_file" if [ $? != 0 ] then echo "$in_file had problems" >> ffmpeg-errors.log fi echo ">Finished "$out_file "\n\n" done done

crie miniaturas no meio do filme.

    
por 13.07.2015 / 20:49