Skip to content

Instantly share code, notes, and snippets.

@DevAmiyo
Last active July 12, 2022 18:45
Show Gist options
  • Save DevAmiyo/fe66f9c8acbfe3d259f951b92ea3b04c to your computer and use it in GitHub Desktop.
Save DevAmiyo/fe66f9c8acbfe3d259f951b92ea3b04c to your computer and use it in GitHub Desktop.
YouTube Thumbnail Generator
:: This is a cmd file to run the code automatically
:: Warning: This only works on Windows
:: For any other operating system, compile and run
:: the java file in terminal manually
@ECHO OFF
TITLE YouTube Thumbnail Generator
:: Compiling the code
javac youtube_thumbnail.java
:: Running the class file
java youtube_thumbnail
PAUSE
import java.util.*; import java.util.regex.*; import java.net.*;
import java.awt.Desktop; import java.io.IOException; // Import modules
public class youtube_thumbnail {
public static void main(String[] args) {
System.out.println("=".repeat(40));
System.out.println(" ".repeat(6) + "YouTube Thumbnail Generator");
System.out.println("=".repeat(40));
Scanner input = new Scanner(System.in);
System.out.print("\nEnter the youtube link to fetch its thumbnail: ");
String link = input.nextLine().trim();
String imgurl = ""; String YTvideoId = extractVideoId(link); int resolution = 0;
String resolutions[] = {"hqdefault", "mqdefault", "default", "maxresdefault",
"hq720", "sddefault", "hq1", "hq2", "hq3",
"mq1", "mq2", "mq3", "1", "2", "3", "sd1", "sd2", "sd3"};
if (YTvideoId == null) {
System.out.println("Invalid URL"); // If the url is invalid
}
else {
System.out.print("\nDefault YouTube Thumbnails\n1. 480 x 360\n2. 320 x 180\n3. 120 x 90\n" +
"\nNote: The following thumbnails may or may not exist \ndepending on the original video quality." +
"\n4. 1920 x 1080\n5. 1280 x 720\n6. 640 x 480\n\n\nOther Thumbnails of the Video" +
"\n7. Thumbnail 1 (480 x 360)\n8. Thumbnail 2 (480 x 360)\n9. Thumbnail 3 (480 x 360)" +
"\n10. Thumbnail 1 (320 x 180)\n11. Thumbnail 2 (320 x 180)\n12. Thumbnail 3 (320 x 180)" +
"\n13. Thumbnail 1 (120 x 90)\n14. Thumbnail 2 (120 x 90)\n15. Thumbnail 3 (120 x 90)\n" +
"\nNote: The following thumbnails may or may not exist \ndepending on the original video quality." +
"\n16. Thumbnail 1 (640 x 480)\n17. Thumbnail 2 (640 x 480)\n18. Thumbnail 3 (640 x 480)\n" +
"\nChoose a thumbnail resolution: ");
int ch = input.nextInt(); // Accepting user input
if (ch > 0 && ch < resolutions.length) {
for (int i = 1; i < resolutions.length; i++) {
if (ch == i)
imgurl = ("https://i.ytimg.com/vi/"+YTvideoId+"/"+resolutions[i-1]+".jpg");
}
System.out.println("\nIf the program does not open the image on your default browser," +
"\nCTRL + Click or Copy and paste the following url in your browser\n"+imgurl);
//Open the image in default browser
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(imgurl));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}
else {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + imgurl);
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
System.out.println("Invalid Option!");
}
}
//Extracts the video id from url
private final static String expression = "(?<=watch\\?v=|/videos/|embed\\/|youtu.be\\/|\\/v\\/|" +
"\\/e\\/|watch\\?v%3D|watch\\?feature=player_embedded&v=|%2Fvideos%2F|embed%\u200C\u200B2F|" +
"youtu.be%2F|%2Fv%2F)[^#\\&\\?\\n]*";
public static String extractVideoId(String videoUrl) {
if (videoUrl == null || videoUrl.trim().length() <= 0) return null;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(videoUrl);
try {
if (matcher.find()) return matcher.group();
} catch (ArrayIndexOutOfBoundsException ex) {
ex.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment