Search code examples
unixffmpegcentos6plesk

To convert the extension of a file to the different directory by a ffmpeg command


The command below is to convert from mp4 file to jpg file with the same file name in the same directory, .../httpdocs/save/.

[root@server-xxxxx-x ~]# for i in `find /var/www/vhosts/xxxxxx.com/httpdocs/save/ -type f -name "*.mp4"`; do ffmpeg -i $i `echo $i | sed -En "s/.mp4$/.jpg/p"`; done


Now, I need to convert from, .../httpdocs/save/ to the different directory, .../httpdocs/file/, how should I change the command above? I'd appreciate if anyone could help me out.

ffmpeg version 2.2.2


Solution

  • Simple method is to execute the command from the directory containing the input files:

    cd "/path/to/inputs" && for i in *.mp4; do ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/${i%.*}.jpg"; done
    

    Or you could use basename if you want to put the full path in the for loop:

    for i in /path/to/inputs/*.mp4; do ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/$(basename "$i" .mp4).jpg"; done
    

    ...but it is less efficient than only using parameter expansion:

    for i in /path/to/inputs/*.mp4; do filepath="${i##*/}"; ffmpeg -i "$i" -frames:v 1 "/path/to/outputs/${filepath%.*}.jpg"; done
    

    Other relevant questions: