logo Debian Debian Debian-France Debian-Facile Debian-fr.org Forum-Debian.fr Debian ? Communautés logo inclusivité

Debian-facile

Bienvenue sur Debian-Facile, site d'aide pour les nouveaux utilisateurs de Debian.

Vous n'êtes pas identifié(e).

#1 23-03-2017 08:28:40

Anonyme
Invité

Assembler des WAV/FLAC/M4A avec un fondu enchainé de 5 secondes

Bonjour,

pour éviter de faire cela avec Audacity, je suis entrain de voir pour le faire avec SOX et BC.
Pour l'instant c'est bien parti.

Le but :


détecter les blancs en entrée et sortie de chaque fichiers et les supprimer.
encodage de tout ça en WAVs resamplés.
Assembler tout ça en un gros fichier Wav unique avec un fondu enchainé de 5 secondes sur tout les morceaux.

à venir ....

Dernière modification par Anonyme (18-07-2019 17:27:52)

#2 23-03-2017 08:51:31

Anonyme
Invité

Re : Assembler des WAV/FLAC/M4A avec un fondu enchainé de 5 secondes

C'est fonctionnel :

Tout d'abord les sources :

http://stackoverflow.com/questions/2865 … -using-sox
http://sox.sourceforge.net/Docs/Scripts

les programmes :


apt-get install sox bc ffmpeg
 



Les deux scripts à rendre exécutables et à copier dans /usr/bin

crossfade_cat.sh


#!/bin/bash
#
# crossfade_cat.sh
#
# Concatenates two files together with a crossfade of $1 seconds.
# Filenames are specified as $2 and $3.
#
# $4 is optional and specifies if a fadeout should be performed on
# first file.
# $5 is optional and specifies if a fadein should be performed on
# second file.
#
# Example: crossfade_cat.sh 10 infile1.wav infile2.wav auto auto
#
# By default, the script attempts to guess if the audio files
# already have a fadein/out on them or if they just have really
# low volumes that won't cause clipping when mixxing.  If this
# is not detected then the script will perform a fade in/out to
# prevent clipping.
#
# The user may specify "yes" or "no" to force the fade in/out
# to occur.  They can also specify "auto" which is the default.
#
# Crossfaded file is created as "mix.wav".
#
# Original script from Kester Clegg.  Mods by Chris Bagwell to show
# more examples of sox features.
#

SOX=/usr/bin/sox
SOXI=/usr/bin/soxi

if [ "$3" == "" ]; then
    echo "Usage: $0 crossfade_seconds first_file second_file [ fadeout ] [ fadein ]"
    echo
    echo "If a fadeout or fadein is not desired then specify \"no\" for that option.  \"yes\" will force a fade and \"auto\" will try to detect if a fade should occur."
    echo
    echo "Example: $0 10 infile1.wav infile2.wav auto auto"
    exit 1
fi

fade_length=$1
first_file=$2
second_file=$3

fade_first="auto"
if [ "$4" != "" ]; then
    fade_first=$4
fi

fade_second="auto"
if [ "$5" != "" ]; then
    fade_second=$5
fi

fade_first_opts=
if [ "$fade_first" != "no" ]; then
    fade_first_opts="fade t 0 0:0:$fade_length 0:0:$fade_length"
fi

fade_second_opts=
if [ "$fade_second" != "no" ]; then
    fade_second_opts="fade t 0:0:$fade_length"
fi

echo "crossfade and concatenate files"
echo
echo  "Finding length of $first_file..."
first_length=`$SOX "$first_file" 2>&1 -n stat | grep Length | cut -d : -f 2 | cut -f 1`
echo "Length is $first_length seconds"

trim_length=`echo "$first_length - $fade_length" | bc`

# Get crossfade section from first file and optionally do the fade out
echo "Obtaining $fade_length seconds of fade out portion from $first_file..."
$SOX "$first_file" -e signed-integer -b 16 fadeout1.wav trim $trim_length

# When user specifies "auto" try to guess if a fadeout is needed.
# "RMS amplitude" from the stat effect is effectively an average
# value of samples for the whole fade length file.  If it seems
# quite then assume a fadeout has already been done.  An RMS value
# of 0.1 was just obtained from trail and error.
if [ "$fade_first" == "auto" ]; then
    RMS=`$SOX fadeout1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
    should_fade=`echo "$RMS > 0.1" | bc`
    if [ $should_fade == 0 ]; then
        echo "Auto mode decided not to fadeout with RMS of $RMS"
        fade_first_opts=""
    else
        echo "Auto mode will fadeout"
    fi
fi

$SOX fadeout1.wav fadeout2.wav $fade_first_opts

# Get the crossfade section from the second file and optionally do the fade in
echo "Obtaining $fade_length seconds of fade in portion from $second_file..."
$SOX "$second_file" -e signed-integer -b 16 fadein1.wav trim 0 $fade_length

# For auto, do similar thing as for fadeout.
if [ "$fade_second" == "auto" ]; then
    RMS=`$SOX fadein1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
    should_fade=`echo "$RMS > 0.1" | bc`
    if [ $should_fade == 0 ]; then
        echo "Auto mode decided not to fadein with RMS of $RMS"
        fade_second_opts=""
    else
        echo "Auto mode will fadein"
    fi
fi

$SOX fadein1.wav fadein2.wav $fade_second_opts

# Mix the crossfade files together at full volume
echo "Crossfading..."
$SOX -m -v 1.0 fadeout2.wav -v 1.0 fadein2.wav crossfade.wav

echo "Trimming off crossfade sections from original files..."

$SOX "$first_file" -e signed-integer -b 16 song1.wav trim 0 $trim_length
$SOX "$second_file" -e signed-integer -b 16 song2.wav trim $fade_length
$SOX song1.wav crossfade.wav song2.wav mix.wav

echo -e "Removing temporary files...\n"
rm fadeout1.wav fadeout2.wav fadein1.wav fadein2.wav crossfade.wav song1.wav song2.wav
mins=`echo "$trim_length / 60" | bc`
secs=`echo "$trim_length % 60" | bc`
echo "The crossfade in mix.wav occurs at around $mins mins $secs secs"
 



MIX.sh


#!/bin/bash

DIR=$(xdg-user-dir DESKTOP)
TMPDIR=/tmp/MIX_MUSIC
OUTPUT=$DIR/$TITLE

JOUR=$(date +%d-%m-%y)
HEURE=$(date +%H.%M.%S)

if [ ! -d $TMPDIR ]; then
mkdir  $TMPDIR


fi


for file in *.flac;
do
sox -S "$file"  "$TMPDIR/CUT_$file" silence -l 1 0.1 1% -1 2.0 1%
#rm "$file"
done

for file in *.flac;
do
sox -S "$TMPDIR/CUT_$file" -r 44100 -b 16 "$TMPDIR/CUT_${file%.flac}.wav";
#rm "$file"
done

crossfade_dur=5
i=0

cd $TMPDIR
for f in *.wav
do

    i=$((i+1))

    if [ $i -eq 1 ]
    then
        cp "$f" $TMPDIR/mix.wav
    else
        crossfade_cat.sh $crossfade_dur $TMPDIR/mix.wav $TMPDIR/"$f" auto auto
    fi

done

 mv $TMPDIR/mix.wav "$OUTPUT/$JOUR à $HEURE[MIX].wav"
 rm -r $TMPDIR

sleep 5

 



Ce placer dans le dossier où se trouvent vos fichiers flacs et ouvrir un terminal et taper:


MIX.sh
 



Le fichier final se trouvera dans le Bureau.

Voilà smile

Dernière modification par Anonyme (18-07-2019 14:58:42)

#3 25-03-2017 08:58:27

Anonyme
Invité

Re : Assembler des WAV/FLAC/M4A avec un fondu enchainé de 5 secondes

Bonjour,

j'aimerai modifier le script MIX.sh.
Mettre la prise en charge de plusieurs formats en plus de flac. (mp3, m4a, ogg) par exemple.
Quelqu'un pour m'aider ? smile

Dernière modification par Anonyme (25-03-2017 08:58:37)

#4 18-07-2019 14:54:22

Anonyme
Invité

Re : Assembler des WAV/FLAC/M4A avec un fondu enchainé de 5 secondes

Déterrage :

crossfade_cat.sh


#!/bin/bash
#
# crossfade_cat.sh
#
# Concatenates two files together with a crossfade of $1 seconds.
# Filenames are specified as $2 and $3.
#
# $4 is optional and specifies if a fadeout should be performed on
# first file.
# $5 is optional and specifies if a fadein should be performed on
# second file.
#
# Example: crossfade_cat.sh 10 infile1.wav infile2.wav auto auto
#
# By default, the script attempts to guess if the audio files
# already have a fadein/out on them or if they just have really
# low volumes that won't cause clipping when mixxing.  If this
# is not detected then the script will perform a fade in/out to
# prevent clipping.
#
# The user may specify "yes" or "no" to force the fade in/out
# to occur.  They can also specify "auto" which is the default.
#
# Crossfaded file is created as "mix.wav".
#
# Original script from Kester Clegg.  Mods by Chris Bagwell to show
# more examples of sox features.
#

#SOX=../src/sox
#SOXI=../src/soxi
SOX=/usr/bin/sox
SOXI=/usr/bin/soxi

if [ "$3" == "" ]; then
    echo "Usage: $0 crossfade_seconds first_file second_file [ fadeout ] [ fadein ]"
    echo
    echo "If a fadeout or fadein is not desired then specify \"no\" for that option.  \"yes\" will force a fade and \"auto\" will try to detect if a fade should occur."
    echo
    echo "Example: $0 10 infile1.wav infile2.wav auto auto"
    exit 1
fi

fade_length=$1
first_file=$2
second_file=$3

fade_first="auto"
if [ "$4" != "" ]; then
    fade_first=$4
fi

fade_second="auto"
if [ "$5" != "" ]; then
    fade_second=$5
fi

fade_first_opts=
if [ "$fade_first" != "no" ]; then
    fade_first_opts="fade t 0 0:0:$fade_length 0:0:$fade_length"
fi

fade_second_opts=
if [ "$fade_second" != "no" ]; then
    fade_second_opts="fade t 0:0:$fade_length"
fi

echo "crossfade and concatenate files"
echo
echo  "Finding length of $first_file..."
first_length=`$SOX --info -D "$first_file"`
echo "Length is $first_length seconds"

trim_length=`echo "$first_length - $fade_length" | bc`

# Get crossfade section from first file and optionally do the fade out
echo "Obtaining $fade_length seconds of fade out portion from $first_file..."
$SOX "$first_file" -e signed-integer -b 16 fadeout1.wav trim $trim_length

# When user specifies "auto" try to guess if a fadeout is needed.
# "RMS amplitude" from the stat effect is effectively an average
# value of samples for the whole fade length file.  If it seems
# quiet then assume a fadeout has already been done.  An RMS value
# of 0.1 was just obtained from trial and error.
if [ "$fade_first" == "auto" ]; then
    RMS=`$SOX fadeout1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
    should_fade=`echo "$RMS > 0.1" | bc`
    if [ $should_fade == 0 ]; then
        echo "Auto mode decided not to fadeout with RMS of $RMS"
        fade_first_opts=""
    else
        echo "Auto mode will fadeout"
    fi
fi

$SOX fadeout1.wav fadeout2.wav $fade_first_opts

# Get the crossfade section from the second file and optionally do the fade in
echo "Obtaining $fade_length seconds of fade in portion from $second_file..."
$SOX "$second_file" -e signed-integer -b 16 fadein1.wav trim 0 $fade_length

# For auto, do similar thing as for fadeout.
if [ "$fade_second" == "auto" ]; then
    RMS=`$SOX fadein1.wav 2>&1 -n stat | grep RMS | grep amplitude | cut -d : -f 2 | cut -f 1`
    should_fade=`echo "$RMS > 0.1" | bc`
    if [ $should_fade == 0 ]; then
        echo "Auto mode decided not to fadein with RMS of $RMS"
        fade_second_opts=""
    else
        echo "Auto mode will fadein"
    fi
fi

$SOX fadein1.wav fadein2.wav $fade_second_opts

# Mix the crossfade files together at full volume
echo "Crossfading..."
$SOX -m -v 1.0 fadeout2.wav -v 1.0 fadein2.wav crossfade.wav

echo "Trimming off crossfade sections from original files..."

$SOX "$first_file" -e signed-integer -b 16 song1.wav trim 0 $trim_length
$SOX "$second_file" -e signed-integer -b 16 song2.wav trim $fade_length
$SOX song1.wav crossfade.wav song2.wav mix.wav

echo -e "Removing temporary files...\n"
rm fadeout1.wav fadeout2.wav fadein1.wav fadein2.wav crossfade.wav song1.wav song2.wav
mins=`echo "$trim_length / 60" | bc`
secs=`echo "$trim_length % 60" | bc`
echo "The crossfade in mix.wav occurs at around $mins mins $secs secs"
 





MIX.sh


#!/bin/bash

DIR=$(xdg-user-dir DESKTOP)
TMPDIR=/tmp/MIX_MUSIC
CROSSFADE=$DIR/Crossfade

JOUR=$(date +%d-%m-%y)
HEURE=$(date +%H.%M.%S)

if [ ! -d $TMPDIR ]; then
mkdir  $TMPDIR

  fi
 
if [ ! -d $CROSSFADE ]; then
mkdir  $CROSSFADE
 
  fi


for file in *.wav;
do

wav_file="$(file -bi "$file" | awk -F "; " '{print $1}' | awk -F "/" '{print $2}')"
if [ "$wav_file" == x-wav ]; then

sox -S "$file"  "$TMPDIR/CUT_$file" silence -l 1 0.1 1% -1 2.0 1%
## sox-14.4.2-4
#sox -S "$TMPDIR/CUT_$file" -r 44100 -b 16 --norm "$TMPDIR/NORMALIZE_${file%.wav}.wav";
## sox-14.4.2-5
sox -S "$TMPDIR/CUT_$file" -r 44100 -b 16 "$TMPDIR/NORMALIZE_${file%.wav}.wav" norm;
rm "$TMPDIR/CUT_$file"


  fi
done




for file in *.flac;
do
flac_file="$(file -bi "$file" | awk -F "; " '{print $1}' | awk -F "/" '{print $2}')"
if [ "$flac_file" == flac ]; then
sox -S "$file"  "$TMPDIR/CUT_$file" silence -l 1 0.1 1% -1 2.0 1%
## sox-14.4.2-4
#sox -S "$TMPDIR/CUT_$file" -r 44100 -b 16 --norm "$TMPDIR/NORMALIZE_${file%.flac}.wav";
## Normalized audio in sox: no such file
##  https://stackoverflow.com/questions/27716230/normalized-audio-in-sox-no-such-file
## sox-14.4.2-5
sox -S "$TMPDIR/CUT_$file" -r 44100 -b 16  "$TMPDIR/NORMALIZE_${file%.flac}.wav" norm;
rm "$TMPDIR/CUT_$file"


   fi
done


for file in *.m4a;
do
m4a_file="$(file -bi "$file" | awk -F "; " '{print $1}' | awk -F "/" '{print $2}')"
if [ "$m4a_file" == mp4 ]; then
ffmpeg -i "$file" "$TMPDIR/WAV_ENC_${file%.m4a}.wav"
sox -S "$TMPDIR/WAV_ENC_${file%.m4a}.wav" "$TMPDIR/CUT_${file%.m4a}.wav" silence -l 1 0.1 1% -1 2.0 1%
rm "$TMPDIR/WAV_ENC_${file%.m4a}.wav"
## sox-14.4.2-4
#sox -S "$TMPDIR/CUT_${file%.m4a}.wav" -r 44100 -b 16 --norm "$TMPDIR/NORMALIZE_${file%.m4a}.wav";
## sox-14.4.2-5
sox -S "$TMPDIR/CUT_${file%.m4a}.wav" -r 44100 -b 16  "$TMPDIR/NORMALIZE_${file%.m4a}.wav" norm;
rm "$TMPDIR/CUT_${file%.m4a}.wav"

   fi
done










 crossfade_dur=5
i=0

cd $TMPDIR
for f in *.wav
do

    i=$((i+1))

    if [ $i -eq 1 ]
    then
        cp "$f" $TMPDIR/mix.wav
    else
        crossfade_cat.sh $crossfade_dur $TMPDIR/mix.wav $TMPDIR/"$f" auto auto
    fi

done

 mv $TMPDIR/mix.wav "$CROSSFADE/$JOUR à $HEURE[CROSSFADE].wav"
 
 
 
 ##  POUR FfFMPEG
##  CHOIX :  (s16le ou s24le)
bits=s16le
##  CHOIX :  (44,1Khz-16Bits ,  48Khz-16Bits , 96Khz-24Bits ou 192Khz-24Bits)
INFO=44,1Khz-16Bits
 


     times=()

 _t=$(ffmpeg -i "$CROSSFADE/$JOUR à $HEURE[CROSSFADE].wav" 2>&1 | awk '/^  Duration/ { split($2, A, ":"); split(A[3], B, "."); print 3600*A[1] + 60*A[2] + B[1] - 5 }')
    times+=("$_t")

echo "${times[@]}" | sed 's/ /+/g' | bc

 ffmpeg -guess_layout_max 0 -i "$CROSSFADE/$JOUR à $HEURE[CROSSFADE].wav" -acodec pcm_$bits  -af "afade=t=in:ss=0:d=1,afade=t=out:st=$times :d=4" "$CROSSFADE/$JOUR à $HEURE[CROSSFADE+IN_OUT].wav"
 
 # https://hoover.gplrank.info/?p=531
 # ffmpeg -i 1.wav -af afade=t=in:st=60:d=4,afade=t=out:st=125:d=5 2.wav
 
 
 rm "$CROSSFADE/$JOUR à $HEURE[CROSSFADE].wav"
 rm -r $TMPDIR

sleep 5
 

Dernière modification par Anonyme (29-05-2020 17:13:09)

Pied de page des forums