r/scripting • u/Kazer67 • Apr 04 '22
[Bash] Automatically get the Width / Height / FPS of all video in a folder and add it to the filename of each one?
EDIT: I achieved my goal finally with documentations from the internet and a lot of trial and error.
So the goal was to add "[width x height - FPS]" to the end of each video automatically. For MP4 I came with this awful but working solution:
for file in *.mp4;
do width=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of csv=s=x:p=0 "$file");
height=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of csv=s=x:p=0 "$file");
fps=$(ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of csv=s=x:p=0 "$file" | sed -r 's/.{2}$//');
mv -- "$file" "${file%.mp4} [$width × $height - $fps FPS].mp4";
done;
Hi all,
I have zero experience in scripting (just some basic Linux knowledge because it's my daily driver) but I'm trying to figure out how to automatically add the resolution and framerate on all the video in a given folder in the video's filename of each one.
Basically to go from:
video.mp4
to
video [1920 x 1080 - 60 FPS].mp4
I found various method to "get" the information (mediainfo, ffprobe, mkvinfo etc) but not how to put it.
So, I think I would need to make a loop and "mv" the files to add " [$width x $height - $framerate FPS]"
I did some research and found how to get the Width en Height and store it with mediainfo:
Width=$(mediainfo --Inform="Video;%Width%" Video.File)&& Height=$(mediainfo --Inform="Video;%Height%" Video.File)
So, I was thinking about:
for vid in *.mp4; do Width=$(mediainfo --Inform="Video;%Width%" Video.File)&& Height=$(mediainfo --Inform="Video;%Height%" $vid); do mv "$vid" "$vid [$Width x $Height]"; done
But then, I think I'll end up with a file like "video.mp4 [$Width x $Height]" instead of "video [$Width x $Height].mp4" and I also still miss the FPS part.
As I said, I know close to nothing about scripting, so if someone can tell me if I'm on the right track or completely wrong?
Thanks in advance for the help!
1
u/can_a_bus Apr 04 '22
I think what you will want is use Bulk Utility Rename Tool. They have a command line to bulk rename files and you can pass in variables to it in a loop.
https://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=4&t=3657
That link is related to what you want.
1
u/64rk Apr 04 '22
I saw a post on /r/PowerShell with the solution to this a week or so again. It's not bash but it can be installed on Linux. If you want that solution I can try to find it again.