Forked from gagannn/Video Thumbnail in Java using Jcodec
Created
January 21, 2021 07:24
-
-
Save faizakram/2f52cd5e5b2e6115bf2a25ea46e842d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.DirectoryStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.UUID; | |
import javax.imageio.ImageIO; | |
import org.jcodec.api.FrameGrab; | |
import org.jcodec.api.JCodecException; | |
import org.jcodec.common.model.Picture; | |
import org.jcodec.scale.AWTUtil; | |
public class VideoThumbnail { | |
public static void main(String[] args) throws IOException, JCodecException { | |
int frameNumber = 0; | |
DirectoryStream.Filter<Path> filter = file -> { | |
return file.toString().endsWith(".mp4") || file.toString().endsWith(".MP4") | |
|| file.toString().endsWith(".mov") || file.toString().endsWith(".MOV"); | |
}; | |
Path dirName = Paths.get("C:\\Users\\14406\\Music\\kiosk"); | |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirName, filter)) { | |
stream.forEach(path -> { | |
try { | |
Picture picture = FrameGrab.getFrameFromFile( | |
new File(path.toString()), frameNumber); | |
BufferedImage bufferedImage = AWTUtil.toBufferedImage(picture); | |
ImageIO.write(bufferedImage, "png", new File( | |
"C:\\Users\\14406\\Music\\kiosk\\video-frame-" + UUID.randomUUID().toString() + ".png")); | |
} catch (Exception e1) { | |
e1.printStackTrace(); | |
} | |
}); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
} | |
// Dependencies to be included in pom.xml | |
<dependency> | |
<groupId>org.jcodec</groupId> | |
<artifactId>jcodec</artifactId> | |
<version>0.2.5</version> | |
</dependency> | |
<dependency> | |
<groupId>org.jcodec</groupId> | |
<artifactId>jcodec-javase</artifactId> | |
<version>0.2.5</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment