How to use Exiftool with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everyone it's franchise923 and i just wanted to make a follow-up to this video where we created a gpx file um using exif tool so xftools this command line tool we can download from the internet and i briefly mentioned in the video how we can use this in python and i just noticed one of our viewers actually uh basically pointed out that when i mentioned that and they they basically asked if we can make a video on this so that's what i'm going to do in this video so yeah if you're interested in using exif tool with python just keep on watching but yeah let's just get right into it so i'm going to show you what i have set up here so i just have a folder here with a bunch of mp4 files in it so we can just play a profile here i am just getting some starbucks how can i help you i could have a grande coffee with cream in it grande coffee with cream is my favorite so you can see we have a bunch of uh mp4 files and then we have these other unrelated or actually they are related but they're not mp4 files this is actually for my dash cam and these glv files get created like side by side with the mp4 files and then you see we have a test folder and i've basically just added the same thing to the test folder and what the script is going to do is we're going to say start in this dash cam footage folder and convert any mp4 that you find and it's going to actually traverse directories and go down into this test folder and if i were to create another folder here i could do the same thing and it's basically recursively going through all the folders so that's what we're going to work on and i wanted to keep these glv files in here just because in our code we're going to want to ignore these we're only interested in the mp4s um so i just wanted to add some unrelated files in here or not they are related but you know files that have different extensions so let's head to python and i'm just going to create a new script here called exif tool gpx creator sounds pretty good to me and yeah the first thing i'm going to do is import os because what we're going to have to do is we need the uh we need to walk it's called in python we need to walk um some directories so the first thing is let's just make a variable um spelled that wrong directory and we're going to make a variable that points to that folder where we have all of our files so i'm just going to right click on any one of them and then hit the uh i'm using a regular keyboard so it's the alt button for me but it's probably the command button for you but i'm just going to copy it as path name like that and if you're on windows we basically just need to do the same thing just copy the path to the folder and i'm going to chop that file off because i just want the actual folder location and the next thing we're going to do is we're going to walk through this directory so it's kind of weird syntax you'll see in a sec but i'm just going to say for path and for directories and files in os dot walk and then give it your root video directory and let's just see what that does for right now so let's just print [Music] path and i'm going to use the f strings here so i can actually put path as a variable in here and then i'm going to do the same thing for directories and this is just so you can see what what this is because it this confused me when i first learned how this worked so i just want to make it clear what this is doing and let's just say files oops uh we have to add the files okay and i'm going to add a new line at the end of this just so there's like a space in between so let's just run this and just see what it gives us so you can see the first thing it gave us was the path and that was the root level path that we gave it so it's starting right at dash cam footage that's what path equals so it's literally just a string that equals that then directories is all the directories that it finds in that path so you can see here path equals dash cam footage directories equals any directory so if there were more directories so if i put a new folder in here called test2 and why don't we just go ahead and do that we'll copy all this stuff into here so now i have a test two test two would have showed up here and then it's listing all the files that are in this location so these are all the files and then the second time it goes through it says okay now we're in the next folder so it's it's uh dug a little deeper into the directory so now it's in test and it's saying well what are the directories in test well there are no directories and tests so that's why it's empty and now it's just listing all the files so let's run this again now that we've added a second test folder and you can see now we're getting basically the same thing for test two so there's no directories but what we could do just to further explain this a little better we could go down into test two add a new folder here called test three and this is probably overkill for what you guys are trying to do but this is just if you had a bunch of directories like nested within each other so now if we run this again we should see where did we make that folder in test so in test two we should see this directories is gonna have something in here so see now test two has a test three directory in it okay so hopefully that makes sense uh it kind of confused me a little bit when i first learned how like what it was doing but uh yeah that's basically what it's doing so what we need to do is we want to loop through the files here so we're going to make a for loop another for loop that says four video file in files and now i'm going to say if video file dot ends with and we're looking for mp4 so if it ends with mp4 print oops print my capstocks on print video file so now we should just get back a bunch of um mp4 files okay so that's that's cool so this is listing every single mp4 file even in all the nested directors so you see there's some repeated because we copied the same ones over and over but that's great like this is exactly what we want now we have a list of the files that we want to run through exif tool so the next thing we can do is we need to basically make a full path to this file because right now this is just the file name and the extension there's no um absolute path like if we just tried to run xftool with this xftool would say where is this file you're just giving me the file name so we need to make another variable called full path full mp4 path and set that equal to os.path dot join and what we're going to do is join the path with the video file so if you remember path is listing the top level root directory so let me just print path here this is and we'll put path in there and this path is going to change every time it digs deeper into another folder it's going to change the path so now we'll say this is full mp4 path all right so let's say 4 path all right let's give this a shot okay it's a little ugly but here's the video file this is path so dash cam footage this is full path dash cam footage and what this os.path.join does it basically takes whatever is on the left and joins it with what's on the right so basically just takes this and adds this to it and it adds a slash so now we have a full path so this is a file this is a valid location we could give the exif tool all right so that makes sense so let's get rid of some of these print statements so now that we know we have all the files we want let's just say print processing and we'll throw in the full mp4 path there okay and what we're going to do is before we actually try to run xftool we need to create an output file so right now we have the input file but now we need to make the output file so full gpx output path equals and we're just going to make this real simple we're going to say full mp4 path dot replace and we're going to replace dot mp mp4 with dot gpx so this this is basically going to say take the mp4 path which is this right here and basically replace mp4 with gpx and that's going to be our output file so that's how we're going to do that all right and now what has to happen we basically need to open this output file because it doesn't exist yet so i'm going to say with open and give it the full output path and then write w for write as gpx file so basically what we're doing here is we're creating this file right here so we're saying with open and then we're saying open this file and it doesn't exist but this is actually going to create it and now what do we need to do we need to construct the exif tool command so if you remember from that video i showed you we had basically an exif tool command line command and we're just going to do the same thing but do it in python so how you do that in python is you give it a list of arguments so i can say exif tool in my command prompt and i get the exif tool shows up like that and if you're on a windows instead of a mac for example um let me just show you it's very similar basically you probably downloaded the exif tool and it's in here and you just need to basically shift and right click and copy as path and then open that up in a command prompt like that and that's the same thing if that makes sense basically exif tool is probably not on your path in windows so if you type exif tool it's not going to know what it is so you can just explicitly tell it where it is and i'm not sure why it's going slow but there you go so that's that's the only difference here just wanted to clear that up so the first prompt or the first uh argument i'm going to give it is exif tool exif tool because again in my command prompt i can just say exif tool now you might have to say exif tool like i just showed you a few seconds ago copy that as path and again this is if you're uh on windows you would go that's what it would look like for you just like that but for me since i'm on mac and it's on my path i'm just going to say exif tool next command is dash ee for i think it stands for extended something extended like extended metadata or something um and then dash m so dash m i think stands for like mute but it's gonna mute any minor warnings you have and i'll show you what i mean in a second actually you know what let's i won't show you that first let's just get this working then dash p and now this is a little tricky we need to give it this gpx.fmt file i think i covered that in this video but basically you need to make a file called gpx.fmt and you can just copy this file like this copy it and then make a file called gpx.fmt i actually have that file right here and you can see [Music] let's open it up with find textedit that's fine just like that so just make sure you have that file i'll link the path to this github file right here and so i'm just going to copy as path name and put it right here all right and then another comma and then we need to give it the output path actually know the input path so full mp4 path all right then so that's just a list right this is all it is is a python list so it's not going to do anything yet this is where we need to use something called sub process in python so i'm going to import that import sub process and this is how you actually run it so subprocess dot run and we're going to run exif tool command what's going on there exif so that's just saying run this so what subprocess dot run does it's a way in python for you to basically say run this in my command prompt so when you say subprocess run python is going to in the background open up its own command prompt like this and run whatever you're telling it to run so it's going to run this so it's going to run xf tool dash ee dash p just like that um okay and then the only other thing that is important here is we need the standard output which is basically what the tool spits out at you we need that to go to a file and that's what we're going to use our gpx file for that's the whole reason we're opening this gpx file we're going to take the standard out and write it to a file um and we're actually telling it to write it to this file all right and that's pretty much it i'm just going to say now print successfully created and let's give it the full gpx output path and i'm just going to add a new line at the end of it oops with slash n that way it's not all bunched up and now when we run this it should uh you should see um gpx files getting created so let's run it okay and see these warnings that we're getting that's what i was talking about earlier if we add the dash m but i'll show you why we're getting this warning first so before that actually now you can see we got gpx files so that's awesome so here you go we have our gpx files all right so the the reason we're getting this minor warning and it says it's minor right here it's because this mp4 file doesn't have this gps altitude piece of metadata um so what we can do to just suppress that or ignore it just if you don't want to see all these warning messages is we're just going to add one more parameter and it's going to be dash m and i'm actually going to put it before that dash p dash m and make sure you put a comma here and i'm i'm guessing it stands for mute but maybe it stands for something like minor i'm not sure but here you go now you can see it's ignoring or not just not displaying that warning message and there you go and the cool thing about that walk is it's converted everything in here so it walked down into the test folder then it walked down in test two and converted all these to gpx's and then it said oh there's another test three folder let's let's uh go down into this and convert everything here and you can see there's these gpx files here all right so there you go that is with 15 lines actually we can even slim this down even more with like 13 lines of code we have uh actually i don't i like having spaces here uh 13 lines of code we have made a script that can recursively convert mp4 files uh to gpx pretty powerful and it's pretty pretty awesome and pretty useful hope that was helpful if you liked it please subscribe and like and subscribe and i can make more content like this because uh this stuff's pretty fun to work with all right thanks for watching
Info
Channel: franchyze923
Views: 124
Rating: 5 out of 5
Keywords: exiftool, gis, qgis, arcgis, mp4, gps, gps mp4 file, extract gps from mp4, mp4 extract metadata, mp4 metadata, go pro metadata, go pro mp4 metadata, python, command line, command line interface, cli, windows cli, shapefile, kml, kmz, gpx, GPX, .GPX, gpx file, embedded metadata, meta data, exiftool windows 10, exiftool tutorial
Id: HXS1FN7ywYg
Channel Id: undefined
Length: 19min 47sec (1187 seconds)
Published: Sun Sep 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.