Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 21/06/2019, à 11:14

gershon09

[Résolu] probleme syntaxe dans un script

Bonjour, j'ai trouvé un script qui m'interesse et je souhaite l'améliorer. J'ai de l'expérience en programation (scientifique, matlab) mais aucune en unix.
Ce que je ne sais pas faire est très basique:

j'utilise la fonction "mogrify" de imagemagick pour annoter une image issue d'une vidéo:
Comme je fais ça en "batch" j'ai besoin de faire des annotations automatiques, avec par exemple le titre qui change selon la vidéo que j'ai analysé.
Donc mes variables sont quelque chose comme le titre du film et sa durée (trouvée par ffmpeg):

FILE='film.mp4'
et :
DUREE=`ffmpeg -i $FILE 2>&1 | gawk 'match($0, /Duration: (.[^,]*)/, matches) {print matches[1]}'`

Ces deux variables changent a chaque film et j'en construit une nouvelle qui en est la synthèse:
annotation="Titre: $FILE Duree: $DUREE"

Pour l'instant j'arrive a faire afficher ça dans mon terminal avec "echo" (c'est un bon début)
echo  $annotation

J'arrive pas à faire ça avec "mogrify". J'arrive à faire aller cette fonction avec un texte simple:

voici mon script, la ligne avec mogrify se situe vers la fin:

#!/bin/bash
#
# Generates thumbnail contact sheets of all video files in current working directory. 
#
# Script defaults to writing PNG contact sheets to the same folder, using the original 
# video filename as the basename for the contact sheet
#
# More details: https://trac.ffmpeg.org/wiki/Create%20a%20thumbnail%20image%20every%20X%20seconds%20of%20the%20video
#
# NOTE: 'montage' requires that Ghostscript be installed, in order to be able to generate titles
# on the contact sheet: run 'brew install ghostscript' to install


function cleanup () {
	
	rm -rf .snapshots/*
}

SNAPSHOT_INTERVAL="120" # time interval (in seconds) to grab each frame


if [ ! -d ".snapshots" ] # check for existence of screengrab temp folder in CWD
  then mkdir .snapshots
fi

shopt -s nocaseglob 	# forces case insensitive extension globbing in the following for loop

for FILE in *.{wmv,avi,rm,ram,mpg,mpeg,mov,mp4,flv,asf};

	do [ -e "$FILE" ] || continue; 



FILESIZE=$(du -h "$FILE" | awk '{print $1}')

		trap cleanup SIGHUP SIGINT SIGTERM 		
		# extract thumbnail frames every $SNAPSHOT_INTERVAL seconds. By telling FFmpeg to set the output files FPS option to
		# a very low value, we made FFmpeg drop a lot of frames at the output, in order to achieve such a low frame 
		# rate, effectively having our thumbnails generated every X seconds
		#
		# thumbnails are written to a hidden .snapshots temp folder in the same folder the script was run in.
		
		echo "------------------------------------------------"
		echo -e "\nExtracting thumbnails for \"${FILE}\"" 
		

ffmpeg -loglevel warning -i "${FILE}" -f image2 -vf fps=fps=1/$SNAPSHOT_INTERVAL .snapshots/._"${FILE}"_%03d.png 2> /dev/null 
		
		# assemble the contact sheet, using ImageMagick's "montage" util
		
		echo -e "Compiling contact sheet for \"${FILE}\""
		montage .snapshots/"._${FILE}"_*.png -geometry +3+1 -title "${FILE}" "${FILE}.png" 
		
		mogrify  -fill blue -pointsize 60 -annotate +60+86 'texte a afficher' "${FILE}.png" 
		
		# purge the temporary .snapshots folder

		echo -e "Cleaning up tempfiles...\n"
		rm -f .snapshots/._"${FILE}"_*.png 

	done 

Ca marche, j'ai une image avec annoté "texte a afficher", voici le résultat dans le terminal:

nico@nico-ThinkPad-E580:~/Public/SCRIPTS$ ./script3.sh
------------------------------------------------

Extracting thumbnails for "film.mp4"
Compiling contact sheet for "film.mp4"
Cleaning up tempfiles...

J'ai voulu faire pareil que ce que j'ai fait avec "echo" mais ça marche pas, voici le script modifié:

#!/bin/bash
#
# Generates thumbnail contact sheets of all video files in current working directory. 
#
# Script defaults to writing PNG contact sheets to the same folder, using the original 
# video filename as the basename for the contact sheet
#
# More details: https://trac.ffmpeg.org/wiki/Create%20a%20thumbnail%20image%20every%20X%20seconds%20of%20the%20video
#
# NOTE: 'montage' requires that Ghostscript be installed, in order to be able to generate titles
# on the contact sheet: run 'brew install ghostscript' to install


function cleanup () {
	
	rm -rf .snapshots/*
}

SNAPSHOT_INTERVAL="120" # time interval (in seconds) to grab each frame


if [ ! -d ".snapshots" ] # check for existence of screengrab temp folder in CWD
  then mkdir .snapshots
fi

shopt -s nocaseglob 	# forces case insensitive extension globbing in the following for loop

for FILE in *.{wmv,avi,rm,ram,mpg,mpeg,mov,mp4,flv,asf};

	do [ -e "$FILE" ] || continue; 


FILESIZE=$(du -h "$FILE" | awk '{print $1}')

		trap cleanup SIGHUP SIGINT SIGTERM 		
		# extract thumbnail frames every $SNAPSHOT_INTERVAL seconds. By telling FFmpeg to set the output files FPS option to
		# a very low value, we made FFmpeg drop a lot of frames at the output, in order to achieve such a low frame 
		# rate, effectively having our thumbnails generated every X seconds
		#
		# thumbnails are written to a hidden .snapshots temp folder in the same folder the script was run in.
		
		echo "------------------------------------------------"
		echo -e "\nExtracting thumbnails for \"${FILE}\"" 
		

ffmpeg -loglevel warning -i "${FILE}" -f image2 -vf fps=fps=1/$SNAPSHOT_INTERVAL .snapshots/._"${FILE}"_%03d.png 2> /dev/null 
		
		# assemble the contact sheet, using ImageMagick's "montage" util
		
		echo -e "Compiling contact sheet for \"${FILE}\""
		montage .snapshots/"._${FILE}"_*.png -geometry +3+1 -title "${FILE}" "${FILE}.png" 
		

		annotation="Title: $FILE"
		mogrify  -fill red -pointsize 60 -annotate +10+26 $annotation "${FILE}.png" 

		# purge the temporary .snapshots folder

		echo -e "Cleaning up tempfiles...\n"
		rm -f .snapshots/._"${FILE}"_*.png 

	done 

Mon image est annotée avec seulement le "Title" qui est dans la variable "annotation". Voici le résultat dans le terminal:

nico@nico-ThinkPad-E580:~/Public/SCRIPTS$ ./script3.sh
------------------------------------------------

Extracting thumbnails for "film.mp4"
Compiling contact sheet for "film.mp4"
mogrify-im6.q16: delegate failed `'ffmpeg' -nostdin -v -1 -i '%i' -vframes %S -vcodec pam -an -f rawvideo -y '%u.pam' 2> '%u'' @ error/delegate.c/InvokeDelegate/1919.
mogrify-im6.q16: unable to extend cache `/tmp/magick-260069JbeCe4bKLMR.pam': Aucun espace disponible sur le périphérique @ error/cache.c/OpenPixelCache/4006.
Cleaning up tempfiles...

On dirait que c'est juste un problème de syntaxe (j'ai beaucoup d'espace disque disponible), le $ devant FILE semble poser problème...

Dernière modification par gershon09 (Le 21/06/2019, à 11:39)


Ma config : Xubuntu 20.04.6 LTS
Lenovo E580, Processeur Intel Core i7-8550U
12 Go de mémoire DDR4, AMD Radeon RX 550 2 Go

Hors ligne

#2 Le 21/06/2019, à 11:29

pingouinux

Re : [Résolu] probleme syntaxe dans un script

Bonjour,
Si ce n'est pas un problème d'espace disque, essaye en mettant des " "

		mogrify  -fill red -pointsize 60 -annotate +10+26 "$annotation" "${FILE}.png" 

Hors ligne

#3 Le 21/06/2019, à 11:36

gershon09

Re : [Résolu] probleme syntaxe dans un script

Bon bah ça marche, c'était une erreur de débutant...
J'aurais du voir ça avant car il y avait "${FILE}.png"  juste au-dessus...

Désolé pour ça :-)
Merci bien !


Ma config : Xubuntu 20.04.6 LTS
Lenovo E580, Processeur Intel Core i7-8550U
12 Go de mémoire DDR4, AMD Radeon RX 550 2 Go

Hors ligne