Identifying Video4Linux devices - Bash Playground

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello if like me you've got more than one webcam attached to your system then you may have experienced the phenomenon of them jumping around which is to say that the device files in dev video aren't guaranteed to stay the same and consistent for instance most the time video 0 will be the internal camera but it's not always the order that they're assigned in is literally to the order that the system discovered the devices and if the system is already booted then that will be the order in which you connected them but if they were all already connected when the system booted then it's a roll of the dice really yeah they're just not guaranteed to be the same now some things about them do stay the same like their name and they have names and the system is aware of them if we have a look at LS USB we can see some names there you go webcam C 270 webcam c920 I think this is the internal camera though why its name wouldn't appear I'm not sure so the question is how do we map these names to these files and the answer lies in the sis directories now the sister rectory is a virtual file system it doesn't exist on any disk but like proc and dev the kernel does kind of in Vincent for us they are used to interact with the kernel at user space essentially here's my shaky understanding so they are devices these webcams and they are going to be under the devices directory but let's not go in there for reasons that will become evident in a moment instead we'll just try and narrow down what we want and say we're looking for video devices we're looking for a class of device so this class and we see there are all different types of device we have printer devices we have LEDs we have firmware GPIO graphics and video for Linux so if we have a look in video for Linux the video for Linux driver has identified three video devices that are attached to the system video 0 video 1 and video to those map up with what we saw in the dev directory now we can't see it there but if we look a little bit closer we'll see that they are actually simoleans into the devices directory the one that I stared its away from because if you look at these paths they're not friendly so it's a lot easier to find what we're looking for through this class rather than directly through sis devices now again it's not very clear from this but those are actually directories and if we have a look inside one of them video 0 there are some more files some of them directories we can't see that at the moment but the only thing we're interested in at the moment is name and that's a file so if we point ourselves at the name file and week at it oh there you go video 0 today is not the internal camera it is the one that's perched up on top of the screen what are the others that's the integrated internal webcam and that is your webcam so great we can get the names of all these devices and we can map them to their device number essentially but how do we now use this information to our advantage well as always by writing a script so I'm going to write a script and I'm gonna put it in my top files bin so that I can get to it it will be called find camera it will be a bin bash script and it will be executable and now we've got highlighting excellent so first off let's just iterate over all the devices we've got and print out their device file and their name so for device in this class the way that he can't have complete here video for Linux star so everything in the video for Linux directory do done so that's our loop now we want the dev path and we're going to have to construct that but that's okay because we've got all the bits so declare dev path equals and little bit string and it will begin with slash dev and then it'll have this bit of the path so because device is going to be the complete path we're going to run it through base name first just to get the end of it so we'll use a process substitution to inject the output of that into the string and say base name device and I know that there won't be any spaces in this path so I'll just leave it as it is we also want the name now because the name is in a file of its own we can read that using the read built-in and a standard in redirection so read name and we're going to redirect in the contents of device echo dev part two name and if we run this find camera there they all are now let's say that we just want the dev path of the camera that matches a bit of the name let us wrap this in a conditional so we can still get the list if we provide no arguments so if dollar hash which is number of arguments is EQ equal to integer comparison zero then you can do that stuff and exit zero because everything was good and we're done with that condition if we do have an argument so if dollar hash EQ one so just the one argument then fee and similar to above we are going to loop over them all so there we go pop that in there and you're not actually going to need to do that so we don't definitely don't need to echo we really need that so if the fragment of name that we've given in argument one exists or is found within the name file so we'll use grep for that if grep q because we don't need grip to output anything if we find our name fragment the first argument in device name then we can just echo out this and actually there's echo out and then exit zero and we're done if we completed and iterated over all of those items and haven't exited at this point then we know that we're not going to find it or we haven't found it there we can exit one and that should be all that's necessary let's get down find camera c920 find camera u b c and web yep there we go now our find camera spoon spawn if you will didn't get any output and the exit code was didn't need to quote that one excellent so there you go there's a little script that will find the device file of a given webcam by its name and my laptop is struggling a little bit with recording and doing this at the same time so I'll sign off I hope that was useful bye you
Info
Channel: Daniel Kendell
Views: 2,991
Rating: 5 out of 5
Keywords: linux, webcam, video4linux
Id: _RC1YF2Tv8Y
Channel Id: undefined
Length: 10min 4sec (604 seconds)
Published: Fri Mar 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.