0

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

user27240
  • 49
  • 10
  • See [this answer](https://stackoverflow.com/a/33766147/1109017) and simply add the directory name to the output. Make sure to run the command from the directory containing the inputs. – llogan Mar 10 '18 at 20:16
  • Thanks, LordNeckbeard, I've tried every possible way, but I cann't figure out where exactly in the command line I should add the second directory which is "/var/www/vhosts/xxxxxx.com/httpdocs/file/". – user27240 Mar 10 '18 at 20:56
  • You don't need to. Just run the command from `/var/www/vhosts/xxxxxx.com/httpdocs/file/` as the current directory (run `cd /var/www/vhosts/xxxxxx.com/httpdocs/file/` then run the command). – llogan Mar 11 '18 at 17:53
  • Thanks, LordNeckbeard, I have Two question. I need to convert from xxxxxx_xxxxxxx.mp4 in .../save/ to xxxxxx_xxxxxxx.jpg file in .../file/. How can I use PHP function, shell_exec('xxxxxx') for this case, "run cd and then run the command"? – user27240 Mar 11 '18 at 21:37
  • Sorry, but I know nothing of PHP. – llogan Mar 11 '18 at 21:58
  • Sorry, LordNeckbeard, I need to convert from xxxxxx_xxxxxxx.mp4 in .../save/ to xxxxxx_xxxxxxx.jpg file in .../file/. Would it be possible? – user27240 Mar 11 '18 at 22:12

1 Answers1

1

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:

llogan
  • 121,796
  • 28
  • 232
  • 243
  • Thanks,LordNeckbeard, I really appreciate your support. The second one from the top is working as perfectly as I could possibly expect in my environment. I also appreciate the list of relevant questions. Thank you for your time and effort on this thread. – user27240 Mar 13 '18 at 13:00