Jump to content

Youtube And Vimeo Thumbnail Grabber


adrian

Recommended Posts

I'm trying this module for the first time but get an error when the module attempts to grab the thumbnail. Same error on localhost and live server. PW 2.7.3

All module settings are at their defaults apart from specifying template to search, URL field and images field.

My URL field:


Error message is

Unable to copy: http://img.youtube.com/vi/NIMgEEASoWQ/maxresdefault.jpg => /home/myproject/public_html/site/assets/files/1053/maxresdefault.jpg

When opening the URL of the thumbnail the module is trying to grab from it looks like this size does not exist. It seems the largest available size is:

http://img.youtube.com/vi/NIMgEEASoWQ/hqdefault.jpg

But is it correct that with the default settings the module is meant to check for the largest available thumbnail and grab that?

Edit: another issue is the one Pete raised previously about selecting an image field to hold the thumbnail - if that field has a limit (e.g. 1 image) the limit is ignored.

I am also curious what you think would be the preferred behavior - I guess if I replicate the admin behavior, adding a new image would replace the last one, but that seems rather ugly if you have checked "All Available" because it will attempt to add each one, but each time deleting the last one for the latest in the options. Same would go for more than one video link in a scanned field - each image would get added, only to be later replaced.

My preference here would be to follow the admin behaviour and replace the existing image. And folks can just have the sense not to check "All available" if they have chosen to use a limited image field. :)

Link to comment
Share on other sites

Hi @Robin S,

Thanks for the report - I have a fix for the "Unable to Copy". However it looks like Youtube has also finally removed direct access to the video's RSS feed for getting the title and description.

I have committed a quick update that takes care of the first problem and prevents errors for the second, so it should work for you now.

I will try to find some time tomorrow to implement the changes needed to fix access to the feed and also look at the max images setting issue.

  • Like 1
Link to comment
Share on other sites

@adrian,

Thanks! So quick and the new commit works great.

Actually, the title was working properly for me (video title was saved to image description field). Although not now with the new commit because I guess you have removed the code for that while you look at implementing something different.

Link to comment
Share on other sites

@adrian,

Thanks! So quick and the new commit works great.

Actually, the title was working properly for me (video title was saved to image description field). Although not now with the new commit because I guess you have removed the code for that while you look at implementing something different.

You're right - I rushed that last update - there are two ways to get the title and the second one still works. I have reinstated that and it should be working again now.

I am still going to wait till tomorrow to deal with the max files setting - I definitely need to think this through a little better first.

  • Like 1
Link to comment
Share on other sites

Thanks @adrian.

I noticed that if the nominated thumbnail field is limited to one image and an image is already loaded there then the image is not replaced with the video thumbnail (but the description is updated to the video title). Is that intentional? I expected the existing image to be replaced. Although I suppose that would create a problem for someone who is wanting to load their own custom video thumbnail in place of the one grabbed from YouTube/Vimeo.

Now that I've used the module a bit I've realised that in most cases I would use a different field setup to what I first tried. Initially I thought I would use an existing field on the page - in my project a user can choose a featured image or a featured video, so I thought it made sense that if a video URL was entered the featured image field contained the video thumbnail. That prompted my mention of the maxFiles issue.

But now I think I want different field settings for the video thumbnail - I want to set it to non-editable because I don't want editors to try and manually override the video thumbnail or think that they need to load a thumbnail image. So I've decided that it's better to create a dedicated image field for the video thumbnail and then use field visibility settings to show/hide image fields based on if the video URL field is populated. I think I would use this kind of setup in future too so the maxFiles thing isn't an issue for me after all (sorry!).

---

Edit: just noticed you already discussed some of this here and the module only replaces an existing image if the image is a video thumbnail grabbed by the module. However there does seem to be a bug. If the video URL field contains a URL and the image field contains a video thumbnail grabbed by the module (i.e. not a manually loaded image), and then the URL is changed to a different video, on page save the image field becomes empty. On a subsequent page save the image field gets the new video thumbnail.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

@Robin S,

Sorry for the delay on this - partly no time, and partly not sure what to do. That bug you noted at the end there is new since I added checks to prevent extra images being added to an image field with MaxFiles not set to '0'.

I am actually thinking that the easiest and best solution is to simply require that the image field that is selected for this module has a maxFiles set to '0'. My reasoning is that there are three scenarios where you might end up with more than one image in the field.

1. You allow users to manually upload other images to that field

2. You are using a textarea field where they can enter multiple video links

3. You have chose "All Available" for the thumbnails to be grabbed

There are also combinations of the above. I don't see a simple, logical way to make this work without the potential for losing images if I continue to allow a single image field and overwrite existing images.

I have gone ahead and made these changes - hopefully it won't impact your (or anyone else's) use cases.

Please let me know if you have any problems with this new version.

  • Like 1
Link to comment
Share on other sites

Hi Adrian,

It's totally fine if you think it's best to require the thumbnail field to have maxFiles '0' - as I mentioned above, I can only see myself using the module with a dedicated image field that I will make non-editable to users, so for me the field will only hold a single image even if maxFiles is set to 0.

But if you wanted to support fields with maxFiles '1' I found that simply reordering the code in your module (the version before your most recent release) fixed the bug. By placing this section...

//Delete any images from videos that were removed from the text fields during this update
if($page->{$this->videoImagesField}){
    foreach($page->{$this->videoImagesField} as $videoImage){

        $imageVideoName = pathinfo($videoImage->filename,PATHINFO_FILENAME);
        if(strpos($imageVideoName, 'video-') === false) continue;

        if($this->whichImages != 'firstAvailable') {
            $imageVideoNameArray = explode('-',$imageVideoName);
            $imageVideoID = str_replace('video-', '', trim(str_replace(array_pop($imageVideoNameArray), '', $imageVideoName),'-'));
        }
        else{
            $imageVideoID = str_replace('video-', '', $imageVideoName);
        }

        if(!in_array($imageVideoID, $allVideos)){
            $page->{$this->videoImagesField}->delete($videoImage);
            $page->of(false);
            $page->save($this->videoImagesField);
        }
    }
}

...above the sections that grab the thumbnails from YouTube/Vimeo any unneeded thumbs are disposed of before the field is repopulated with the new thumbnail. But I didn't do extensive testing so there are perhaps consequences to reordering the code like this.

  • Like 1
Link to comment
Share on other sites

Thanks - yeah, it is certainly doable, but the problem I see is that if the dev sets the max to 1 and doesn't prevent users from manually uploading an image, and this modules works like PW in that a single image is replaced if the max is one, then manually uploaded image will be lost. So it doesn't work for the scenario where you want the user to be able to override the default image from Youtube/Vimeo. I think it's better in that scenario to have a dedicated "manual override" image field - at least that is my current thinking.

Is there anyone else there with any thoughts on this? I'd like to get a consensus before I go making anymore changes.

  • Like 1
Link to comment
Share on other sites

I think you already solve that scenario with

if(strpos($imageVideoName, 'video-') === false) continue;

So a manually uploaded image will not be replaced (well, unless the image name by chance included the string 'video-' but that's unlikely). And that makes total sense to me. It wasn't what I expected at first but now I understand the intention behind it I see how it's the best option. And this behaviour could be mentioned on the module config screen or module description to clear up any confusion.

  • Like 1
Link to comment
Share on other sites

That make sense, but here's another scenario for you. What if the dev sets the maxFiles to 3 and the Which Images to All Available. And it is attached to a textarea field where they can enter multiple videos - what images get kept, and which ones get deleted when additional video links are added?

Obviously it would be a silly combination to set up, but you never know. At least the way it is now if you want just one video per page you can use a text field (not textarea) and choose First Available. That way you'll only ever get one image - the only caveat is that you need to do $page->images->first(). If you want the manual upload override you can have a separate field, or you could tell the site editors to simply order the images field so that their manually uploaded one is in first position. Or, the template logic could look for the presence of an image without the "video-" prefix and use that if it exists?

I am still not convinced I am right, so I would still like to hear from other users of this module!

Thanks for your continued thoughts on this!

  • Like 1
Link to comment
Share on other sites

At least the way it is now if you want just one video per page you can use a text field (not textarea) and choose First Available. That way you'll only ever get one image - the only caveat is that you need to do $page->images->first(). If you want the manual upload override you can have a separate field, or you could tell the site editors to simply order the images field so that their manually uploaded one is in first position.

Sounds good to me. :)

  • Like 1
Link to comment
Share on other sites

  • 11 months later...
3 minutes ago, houseofdeadleg said:

I'm using this module on a site I'm developing, it works fine on the local copy, but on the development server it can't seem to grab the images from Youtube.

Most likely you need to turn on allow_url_fopen in your php.ini file.

Link to comment
Share on other sites

2 hours ago, adrian said:

Most likely you need to turn on allow_url_fopen in your php.ini file.

I've checked and that's already turned on. The development server's running a multi-site install of PW, one of the other sites on the server has the exact same st up for videos/thumbnails and that works without any issue.

Link to comment
Share on other sites

6 hours ago, houseofdeadleg said:

I've checked and that's already turned on. The development server's running a multi-site install of PW, one of the other sites on the server has the exact same st up for videos/thumbnails and that works without any issue.

Not sure where to start on this one - do you have debug mode turned on / Tracy Debugger installed? If not try those and see if any errors/warnings are returned. Also, have you checked different YouTube videos on the site that isn't working to make sure it's not just one video? What about Vimeo videos - do thy work? After that, I think you might need to dive into the module and see if you can figure out where it is failing. 

Link to comment
Share on other sites

2 hours ago, rooofl said:

@adrianThanks for this useful module! Can you confirm it doesn’t work in repeaters fields? Can you suggest a workaround?

Hi @rooofl - actually it works just fine with repeater fields. You just need to specify the template of the repeater field itself, not the template of the page when specifying "Templates to Search". 

Let me know if you still have troubles.

Link to comment
Share on other sites

  • 6 months later...
On 16-4-2017 at 10:40 AM, adrian said:

Hi @rooofl - actually it works just fine with repeater fields. You just need to specify the template of the repeater field itself, not the template of the page when specifying "Templates to Search". 

Let me know if you still have troubles.

Hi @adrian, I just took a look at this specific behavior, and found that while it does work, it does not work as I expected. 

Expected:
Add repeater, each row has an URL field.
Also add a single Images field, to collect the images. 
Video images load into central images field on page.

Actual:
Add a repeater, each row has a URL field and an Image field.
Video images load within each repeater row's Image field

I think it makes sense for the module to check it it is running on a repeater, and then (optionally) trying to load the images in an Images field inside the parent template.
I'll try to implement this.

  • Like 1
Link to comment
Share on other sites

7 minutes ago, eelkenet said:

I think it makes sense for the module to check it it is running on a repeater, and then (optionally) trying to load the images in an Images field inside the parent template.
I'll try to implement this.

Hi @eelkenet - I haven't used this module in quite a while and actually never personally used it with repeaters, but I can definitely see why you'd prefer this behavior. Thanks for having a go at implementing this. I think that while this behavior is probably preferred by most users, I agree that making it optional is best, especially because this could be a breaking change for existing users. 

Looking forward to a PR - thanks!

Link to comment
Share on other sites

4 hours ago, adrian said:

I think that while this behavior is probably preferred by most users, I agree that making it optional is best, especially because this could be a breaking change for existing users. 

Maybe I'm missing something, but I don't understand how this can work. If video URLs are entered within a Repeater field then presumably there are multiple videos on the page. So how is the connection between video URL and video thumbnail going to work, given that the sort order within the Repeater and Images field can both be changed independently by the user?

  • Like 1
Link to comment
Share on other sites

Hi @Robin S - excluding repeaters for a minute for the sake of clarity, it is already possible to have multiple videos on the one page, either in multiple fields, or within a texarea field, so images in the images field could be from multiple videos. I think I assumed that the dev would always grab the thumbnail for a video by name, rather than it's order in the images field, but I don't really know how this is being used, so hard to know.

Maybe others can chime in because as I mentioned I don't think I have actually haven't ever used this module - I built it for request here in the forum.

 

Link to comment
Share on other sites

2 minutes ago, adrian said:

I think I assumed that the dev would always grab the thumbnail for a video by name, rather than it's order in the images field

That's certainly doable within a template but not simple. You'd have to parse the URL to extract the video ID, convert it to lowercase, add the "video-" prefix and then match it against the image field. I've used the module within a Repeater several times and personally I prefer having a dedicated image field within the Repeater so I can see the thumbnail right alongside the URL. So much easier to identify the thumbnail that way, both visually within Page Edit and in the template file.

  • Like 1
Link to comment
Share on other sites

12 hours ago, Robin S said:

If video URLs are entered within a Repeater field then presumably there are multiple videos on the page. So how is the connection between video URL and video thumbnail going to work, given that the sort order within the Repeater and Images field can both be changed independently by the user?

For my use case, I want to give my user the possibility to add images and videos to a (sortable) fullscreen slideshow on the front-end.

Of course I can accomplish this using simple repeaters, or a repeater matrix, or a page table or something like that, meaning each slide comes from a single element inside. 
However, when dealing with a large amount of images (batch upload) this gets really annoying, after all you need to upload each image individually - just to be able to also add videos.

So to me it makes total sense to add the video-preview images to the same Images field as I use for the other images. 

Technically I would use the order of the Images to define the order of my slides, and then when outputting this into my API check if the filename fits the expected video-ID format and search for the corresponding Video URL.   

A completely different solution would be to allow for batch creation of repeater matrix items, from a multiple file upload. But that seems like a lot more work to achieve. 

 

16 hours ago, adrian said:

Looking forward to a PR - thanks!

I already have a working version locally, but it is a bit messy. I'll try to clean it up a bit and create that PR for you in the near future.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...