3

I am using vlcj to capture the screen in my Java program. Therefore I use the following code:

public static void main(final String[] args) {
            NativeLibrary.addSearchPath("vlc", "/Applications/VLC.app/Contents/MacOS/lib/");
            SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                new CaptureTest().start("screen://");
              }
            });
          }

public CaptureTest() {
            factory = new MediaPlayerFactory();
            mediaPlayer = (HeadlessMediaPlayer) factory.newMediaPlayer();
          }

          private void start(String mrl) {

            File dir = new File(System.getProperty("user.home"), "Videos");
            dir.mkdirs();

            DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String fileName =  dir.getAbsolutePath() + "/Capture-" + df.format(new Date()) + ".mp4";

            String[] options = {
            ":sout=#transcode{vcodec=h264,acodec=mp4a}:std{mux=mp4,access=file,dst=" + fileName + "}", ":input-slave=screen://"
            };

            mediaPlayer.playMedia(mrl, options);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mediaPlayer.stop();
            mediaPlayer.release(); 
          }

The problem is that the video output file is only 4KB and you can't play it. Can anyone help me? I am on Mac OS 10.6.8 and I use VLC 1.1.12 and vlcj 1.1.5

ee.
  • 947
  • 5
  • 5
tester
  • 3,977
  • 5
  • 39
  • 59
  • Do you mean you want to do screencasting with VLC? – ee. Jan 31 '12 at 05:33
  • Have you tested the :sout command option via the VLC command itself rather than Java to see whether it works correctly or not on your PC setup? – ee. Jan 31 '12 at 05:39
  • If it doesn't work, please try the VLC command line option given in the following link http://opensource.about.com/od/tutorials/ss/How-To-Capture-A-Screencast-Using-Vlc_6.htm – ee. Jan 31 '12 at 05:40
  • If the `dst` parameter in the :sout command option has a filename with spaces in between, you may need to escape the double quotes as follows `"dst=\"" + filename + "\""` – ee. Jan 31 '12 at 05:42
  • No I don't want to screencast. I want to record my desktop. I tested the following on console (Mac OS): /Applications/VLC.app/Contents/MacOS/VLC screen:// --sout="#transcode{vcodec=h264,acodec=mp4a}:std{access=file,mux=mp4,dst=/User/test/Desktop/test.mp4}" This worked as expected, it recorded my screen. The filename has no spaces in it. Maybe you can give me an Java example where it works with mp4 and h264? – tester Jan 31 '12 at 07:40
  • actually the vlc command from http://opensource.about.com/od/tutorials/ss/How-To-Capture-A-Screencast-Using-Vlc_6.htm worked fine. it recorded the screen. but in a very poor quality. The quality with the command i posted above is much better, but it doesnt work (on console it works). what am i doing wrong? – tester Jan 31 '12 at 08:24
  • I modified the :sout command from the link. It works fine on my PC system. It is able to record the desktop screen into a mp4 file. The modified :sout command: `":sout=#transcode{vcodec=h264,venc=x264{scenecut=100,bframes=0,keyint=10},vb=1024,acodec=none,scale=1.0}:duplicate{dst=display,dst=std{access=file,mux=mp4,dst=\""+filename+"\"}}" ` – ee. Jan 31 '12 at 09:50
  • maybe, you can set none for `acodec` parameter for transcode part first and see what happens. – ee. Jan 31 '12 at 09:52
  • For the record, I am using libVLC library and plugins from the stable VLC 1.1.11 for Windows OS via VLCJ 1.2.0 – ee. Jan 31 '12 at 09:53
  • There is a possible problem in your original code; check my possible answer... – ee. Jan 31 '12 at 10:24

2 Answers2

1

Here is the interesting part of your code...

    mediaPlayer.playMedia(mrl, options);
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    mediaPlayer.stop();
    mediaPlayer.release(); 

Why did you play the media, slept for 5 seconds and stopped immediately after that? Maybe, this is why you get a very small file size during the screen recording. From what I notice, the transcode is not so fast enough, so the file is not increasing in size immediately (maybe due to buffering takes place during transcoding part, I guess...)

The best is to create a button each for the play/record action and for the stop action.

ee.
  • 947
  • 5
  • 5
  • no, thats definitely not the problem. i found it out myself: i had to add a screen-fps=5.0 option to the options array. after it worked fine. – tester Jan 31 '12 at 13:49
  • Hmm, is it really as simple as that? Because, I don't need to set `screen-fps` at all to succesfully record a desktop screen via VLCJ in my sample code. When I run your code, it is obviously the part I've shown to you doesn't record a file properly since it has been cut short too early (runs for 5 seconds and later stops immediately). That is why the file is just 4KB and doesn't play. For a single frame of 640 x480 x 32 pixels to be recorded, it shall at least produce 9MB size uncompressed. Maybe, you have overlooked something and got lost in finding a solution to your real problem. – ecle Jan 31 '12 at 14:50
  • Correction: I forgot to divide by 8...if uncompressed, it shall be at least 1MB (640 x 480 x 32 /8). 32 bit color/8 = 4 bytes information times the resolution size of a frame... I don't know your desktop size at the time you record it..so, I just assume 640 x 480. But h264 codec involves reduction of information to frame size from one frame state to another if compression takes place. – ecle Jan 31 '12 at 15:05
  • Continued... But, I don't think it as a little as 4KB :) – ecle Jan 31 '12 at 15:11
0

You might check your sout chain. I also had problem with video file generation. The working sout chain for me is :

:sout=#transcode{vcodec=mp2v, vb=4096,scale=1, acodec=mpga, ab=128, channels=2, samplerate=44100}
:duplicate{dst=file{dst="+ fileName+"}, dst=display, select=noaudio}
Adil
  • 4,503
  • 10
  • 46
  • 63
iltaf khalid
  • 9,928
  • 5
  • 30
  • 34