I want to write a program that converts video into frames using FFMPEG. When I use it on the Ubuntu terminal, it works fine. But when I try to put it into the Java code, it gives me a runtime error. Did I make a mistake in my code below?
import java.util.*;
import java.awt.*;
import java.lang.*;
import java.lang.Runtime;
import java.io.*;
import java.io.IOException;
public class ConvertVideoToImage
{
private SingletonServer ss = null;
public ConvertVideoToImage(SingletonServer ss)
{
this.ss = ss;
}
public void run()
{
convertVideo();
}
public void convertVideo()
{
try
{
Runtime rt = Runtime.getRunTime().exec("ffmpeg" + "-i" + "display.wmv" + "image%d.jpg");
}
catch(Exception e){}
}
}
Edit:
I have changed the code like you suggested, but it also doesn't work. And when I Googled it, I found out that someone put the full path inside the executable and it became like this:
Runtime.getRuntime().exec("/home/pc3/Documents/ffmpeg_temp/ffmpeg -i display.wmv image%d.jpg")
BTW, thanks for the reply. I have another question. Is it possible to make a counter for FFMPEG? I used this command in the Ubuntu terminal to make it convert a video to 30 frames/1seconds:
ffmpeg -i display.wmv image%d.jpg
This will automatically generate numbers like image1.jpg, image2.jpg, to image901.jpg. Is it possible to make a counter for this? Because I need to count the files and control the number.
Thanks in advance.
When you call exec, you should not specify the parameters in the command string, instead, pass them in an array as second parameter.
Process p = Runtime.getRunTime().exec("ffmpeg",
new String[]{"-i", "display.wmv", "image%d.jpg"));
// are you sure regarding this %^
Related
Ok, before anyone starts flaming me for asking "dumb" questions, please note that I have pretty much exhausted every other option that I know of and come up empty handed. Still if you feel like it, please go ahead and downvote/report this question.
Now, for those that care
I am trying to take a String input from user and store it into a file Text.txt which will be created in the current working directory.
Following is the code
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
public class Encryption {
public static void main(String[] args) throws Exception {
System.out.println("Enter a String you wish to encrypt : ");
new BufferedWriter(new FileWriter(".\\Text.txt")).write(new Scanner(System.in).nextLine());
System.out.println("Done");
}
}
My problem is, the file is getting generated at the correct destination, but is always empty. I have tried it on multiple JDK versions and on different machines. Still getting the blank text file.
Please tell me, what is it that I am doing wrong.
You are not closing with .close() the BufferedWriter (which would then flush the last buffer and close the file).
You can however do that task in new style:
Files.write(Paths.get(".\\Text.txt"),
Arrays.asList(new Scanner(System.in).nextLine()),
Charset.defaultCharset());
Otherwise you would need to introduce a variable, and gone is the one-liner.
Some changes i made your code to work
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
public class Encryption {
public static void main(String[] args) throws Exception {
System.out.println("Enter a String you wish to encrypt : ");
String text = new Scanner(System.in).nextLine();
BufferedWriter b = new BufferedWriter(new FileWriter(".\\Text.txt"));
b.write(text);
b.close();
System.out.println("Done");
}
}
I am programming using Java to connect to Jenkins instance and fetch/post few information. I am using jar which allows me to use methods directly to fetch information about job(name of job, pass/fail status, job description etc). When I use any method which return boolean or number or url, it works fine. But when return type is String it gives me output in a different format.
Find the code below:
import com.offbytwo.jenkins.*;
import com.offbytwo.jenkins.client.*;
import com.offbytwo.jenkins.model.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
public class NewTest {
public static void main(String[] args) throws URISyntaxException, IOException {
System.out.println("begin");
JenkinsServer server = new JenkinsServer(new URI("https://my-jenkins.abc.com/job/MyJobs/job/Dev/job/Test/"), "userid", "token");
Map<String, Job> jobs = server.getJobs();
for (Map.Entry<String, Job> entry : jobs.entrySet())
{
String name = entry.getKey();
JobWithDetails jobdet = jobs.get(name).details();
System.out.println("Job Links");
System.out.println(jobdet.getUrl());
System.out.println("Next Build Number");
System.out.println(jobdet.getNextBuildNumber());
System.out.println("Detail by build number");
System.out.println(jobdet.getBuildByNumber(1));
}
}
Output:
after begin
Job Links
https://my-jenkins.abc.com/job/MyJobs/job/Dev/job/Test/job/git_test/
Next Build Number
4
Detail by build number
com.offbytwo.jenkins.model.Build#15888e2
How do I get "Detail by build number" in proper format? I used toString() function but it threw me error.I was getting same format error with JobWithDetails jobdet = jobs.get(name).details();
JobWithDetails jobdet = jobs.get(name).details().toString();
It still gives me following output:
com.offbytwo.jenkins.model.Build#15888e2
I tried defining toString() separately but I did not know what should I return and toString() I defined outside cannot access variables inside main function. How do I fix this?
Thank you.
You are trying to print the entire Build object, which (as you've noticed) does not have toString() overridden. Thus it prints the unhelpful stuff you see there.
You need to decide what property(ies) of Build you want to print. If, for example, it's the build URL that you want, you will want to print this:
System.out.println(jobdet.getBuildByNumber(1).getUrl());
I want to write a program that copies one file to another. I got my program to execute and run but nothing happens! I have no errors to go by so I'm stuck and don't know what to do! It doesn't create the files or copies them into one file.
Here's the command I typed:
java CopyFile report.txt report.sav
The program should create another copy of the file report.txt in report.sav. Your program should print the following error message for inappropriate number of input arguments (for e.g., java CopyFile report.txt):
Here's my code:
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
/**
This program copies one file to another.
*/
public class CopyFile
{
public static void main(String[] args) throws FileNotFoundException
{
if (args.length != 2)
{
System.out.println("Usage: java CopyFile fromFile toFile");
return;
}
String source = args[0];
}
}
use this-
Files.copy(source.toPath(), dest.toPath());
This method you can find in java 7.
Refer to this link for other ways-
http://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/
You can use FileUtils from Apache IOCommons
FileUtils.copyFile(src, dest)
I am trying to write a Java UDF with the end goal of extending/overriding the load method of PigStorage to support entries that take multiple lines.
My pig script is as follows:
REGISTER udf.jar;
register 'userdef.py' using jython as parser;
A = LOAD 'test_data' USING PigStorage() AS row:chararray;
C = FOREACH A GENERATE myTOKENIZE.test();
DUMP D;
udf.jar looks like:
udf/myTOKENIZE.class
myTOKENIZE.java imports org.apache.pig.* ande extends EvalFunc. the test method just returns a Hello world String.
The problem that I am having is that when I try to call the method test() of class myTOKENIZE I get Error 1070: ERROR 1070: Could not resolve myTOKENIZE.test using imports: [, java.lang., org.apache.pig.builtin., org.apache.pig.impl.builtin.] Thoughts?
As your UDF extends EvalFunc there should me a method called exec() in the class myTOKENIZE.
Your pig code would then look as follows:
C = FOREACH A GENERATE udf.myTOKENIZE(*);
Please read http://pig.apache.org/docs/r0.7.0/udf.html#How+to+Write+a+Simple+Eval+Function
Hope that helps.
So is myTOKENIZE in the package udf? In that case you'd need
C = FOREACH A GENERATE udf.myTOKENIZE.test();
After waaaaay too much time (and coffee) and a bunch a trial and error, I figured out my issue.
Important note: For some jar myudfs.jar, the classes contained within must have package defined as myudfs.
The corrected code is as follows:
REGISTER myudfs.jar;
register 'userdef.py' using jython as parser;
A = LOAD 'test_data' USING PigStorage() AS row:chararray;
C = FOREACH A GENERATE myudfs.myTOKENIZE('');
DUMP C;
myTOKENIZE.java:
package myudfs;
import java.io.IOException;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
import org.apache.pig.impl.util.WrappedIOException;
public class myTOKENIZE extends EvalFunc (String)
{
public String exec(Tuple input) throws IOException {
if (input == null || input.size() == 0)
return null;
try{
String str = (String)input.get(0);
return str.toUpperCase();
}catch(Exception e){
throw WrappedIOException.wrap("Caught exception processing input row ", e);
}
}
}
the structure of myudfs.jar:
myudfs/myTOKENIZE.class
Hopefully this proves useful to someone else with similar issues!
This is very late but I think the solution is that while using the udf in your pig you have to give fully qualified path of the class with your package name.
package com.evalfunc.udf; and Power is my class name as
public class Power extends EvalFunc<Integer> {....}
Then while using it in pig first register the jar file in pig and then use the udf with full package name like:
record = LOAD '/user/fsbappdev/maitytest/pig/pigudf/power_data' USING PigStorage(',');
pow_result = foreach record generate com.evalfunc.udf.Power(base,exponent);
I am learning java with BlueJ, and recently I was given a .jar file called Imagen.jar. Apparently, what it does is return some pixel vectors depending on image file names given as parameters to it.
Anyway, I am supposed to make a program that will use a class called Imagen. Apparently, such class is within the mentioned .jar file.
Clearly, BlueJ won't compile if I'm using such class since I have not imported it or anything. But, I don't really know how to import such class in the first place.
I was given the following example code:
import java.io.PrintWriter;
public class Main {
public static void main(String arg[ ]){
if(arg.length > 1){
Imagen imagen = new Imagen(arg[0]);
int [][] m = imagen.getMatriz();
PrintWriter salida = null;
try {
salida = new PrintWriter(arg[1]);
}
catch(Exception e){
System.err.println(e);
}
for(int [] fila : m ){
for(int valor : fila){
System.out.print("\t"+valor);
salida.print("\t"+valor);
}
salida.println("");
System.out.println("");
}
if(salida!=null){
salida.close();
}
}
else {
System.out.println("Uso: java -classpath .;Imagen.jar Main nombreArchivo.gif");
}
}
}
Which does not compile using BlueJ. However, as you can see, at the end it says that to use it, you have to type in the terminal:
java -classpath .;Imagen.jar Main myImageFile.gif
And I do it. But it keeps throwing me the same message.
So I am stuck right now:
Why is the terminal line I was told to use not working?
How can I import the class that is contained within a .jar file?
You need to do the following once.
Select the menu option Tools -> Preferences.
In the resulting dialog, click on the Libraries tab.
Click the Add button.
Navigate to the folder containing jar file. Select jar file.
Restart BlueJ.
Answer extracted from this place
you need to import Imagen class as it is being used in the main method.