Newest 'ffmpeg' Questions - Stack Overflow

http://stackoverflow.com/questions/tagged/ffmpeg

Les articles publiés sur le site

  • How OpenGL directly manipulates data on GPU that a video hardware decoded by FFmpeg CUDA

    24 janvier 2019, par gn cavalry

    Ubuntu 18.04
    FFmpeg 4.1
    CUDA 10.0

    What I want to do is using FFmpeg CUDA decode a video, directly peocess data on GPU by GLSL , then encode data using FFmpeg CUDA. Like this:

    Raw Video --> FFmpeg CUDA decoder --> GLSL --> FFmpeg CUDA encoder --> New Video

    Here is the short code:

    ret = avcodec_send_packet(decoder_ctx, pkt);
    while (ret >= 0) {
        ret = avcodec_receive_frame(decoder_ctx, frame);
    
        /**
         * Now CUDA decoded data is stored on GPU and referenced by frame
         * How can GLSL manipulate decoded data on GPU directly, 
         * without transfer it to CPU and load to GPU again by glTexImage2D()
         */
    
    
        // I don't if the following code is right, if GLSL processed data is still referenced by frame
        while (avcodec_send_frame(encoder_ctx, frame)) {
            avcodec_receive_packet(encoder_ctx, &enc_pkt);
        }
    }
    

    Can anyone help me? Thanks very much!

  • how to send audio or video by packet though udp and sync the iamge and audio

    24 janvier 2019, par Wei Wen

    how to send part of video and audio from mp4 as packet though udp from server Client will play the part of packet resevice.

    import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.math.BigInteger; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.ServerSocket; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.ShortBuffer; import java.util.ArrayList; import java.util.Arrays; import javax.imageio.ImageIO; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioFormat; import javax.swing.JTextArea;

    import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.Frame; import org.bytedeco.javacv.Java2DFrameConverter;

    import Enum.EType.ClientState; import View.SingleDisplayWindow;

    import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException;

    import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.bytedeco.javacv.FrameGrabber;

    public class SCon { private final static int PORT = 8888;

    private final JTextArea TEXT_AREA; private volatile SingleDisplayWindow DISPLAY; /////

    private final String BD_USER_NAME, DB_PASSWORD; private Database database;

    private boolean isRunning;

    private RSA serverRSA, clientRSA;

    private int keyIndex, typeID = 0; private String mediatype = "";
    private ArrayList sHandlers;

    private FileStreamingThread fileStreamingThread; private VideoStreamingThread videoStreamingThread; private BroadcastThread broadcastThread; private ConnectThread connectThread;

    private volatile static byte[] currentVideoFrame = new byte[0], currentAudioFrame = new byte[0]; // current image music

    public void run() { startServer();

      isRunning = true;       fileStreamingThread = new
    

    FileStreamingThread(videoFile); videoStreamingThread = new VideoStreamingThread(videoFile); //CountDownLatch latch = new CountDownLatch(1); fileStreamingThread.start(); videoStreamingThread.start(); //latch.countDown();

              broadcastThread = new BroadcastThread();        broadcastThread.start();
    
      connectThread = new ConnectThread();        connectThread.start();  }
    

    public void stop() { isRunning = false;

      try {           new Socket("localhost", PORT);
    
      } catch (IOException e) {           e.printStackTrace();        }
    
      while (fileStreamingThread.isAlive()) {
    
      }
    
      while (broadcastThread.isAlive()) {
    
      }
    
      while (connectThread.isAlive()) {
    
      }
    
      for (SHandler sHandler : sHandlers) {           sHandler.connectionClose();
      }       sHandlers.clear();      DISPLAY.dispose();
      TEXT_AREA.append("\nServer stop\n");    }
    
    
      private class VideoStreamingThread extends Thread {         private
    

    FFmpegFrameGrabber grabber; // Used to extract frames from video file. private Java2DFrameConverter converter; // Used to convert frames to image private int curIndex; // Current key index

      public VideoStreamingThread(String video_file) {            videoFile =
    

    videoFile; grabber = new FFmpegFrameGrabber(videoFile); converter = new Java2DFrameConverter(); try { grabber.restart();

          } catch (FrameGrabber.Exception e) {
              e.printStackTrace();            }           curIndex = keyIndex;        }
    
      public void run() {             try {
    
              while (isRunning) {
                  curIndex = keyIndex;
                  Frame frame = null;
                  System.out.println("v1");
                  if ((frame = grabber.grab()) != null) { // Grab next frame from video file
                      if (frame.image != null) { // image frame
    
                          BufferedImage bi = converter.convert(frame); // convert frame to image
    
                          // Convert BufferedImage to byte[]
                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
                          ImageIO.write(bi, "jpg", baos);
                          // Encrypt data and store as the current image of byte[] type
                          currentVideoFrame = ciphers[curIndex].doFinal(baos.toByteArray());                                                                          
                          //////////////////
                          DISPLAY.setSize(new Dimension(bi.getWidth(), bi.getHeight()));
                          DISPLAY.updateImage(bi); // Display image 
                      //  Thread.sleep((long) ( 999 / grabber.getFrameRate()));
    
                          ///////////////
                          typeID = 1;
                          mediatype = grabber.getFormat();
    
                      } 
                  } else {
                      grabber.restart();
                  } // Restart when reached end of video
              }
              grabber.close();
    
          } catch (IOException e) {
              e.printStackTrace();
    
          } catch (IllegalBlockSizeException e) {
              e.printStackTrace();
    
          } catch (BadPaddingException e) {
              e.printStackTrace();
    
          }           //catch (InterruptedException e) {e.printStackTrace(); }        }
    
      public synchronized int getCurKeyIndex() {          return curIndex;        }
    
      public synchronized void getVideoFile(String video_file) {
          videoFile = video_file;             grabber = new
    

    FFmpegFrameGrabber(video_file); converter = new Java2DFrameConverter();

          try {
              grabber.release();
              grabber.restart();
    
          } catch (FrameGrabber.Exception e) {
              e.printStackTrace();            }       }   }       private class FileStreamingThread extends Thread {      private FFmpegFrameGrabber
    

    grabber; // Used to extract frames from video file. private int curIndex; // Current key index

      public FileStreamingThread(String video_file) {             videoFile =
    

    videoFile; grabber = new FFmpegFrameGrabber(videoFile); try { grabber.restart();

          } catch (FrameGrabber.Exception e) {
              e.printStackTrace();            }           curIndex = keyIndex;        }
    
      public void run() {             try {
    
              while (isRunning) {
                  curIndex = keyIndex;
                  Frame frame = null;
    
                  System.out.println("a2");
                  if ((frame = grabber.grabSamples()) != null) { // Grab next frame from video file
                      if (frame.samples != null) { // audio frame
                          // Encrypt audio
    
                          ShortBuffer channelSamplesShortBuffer = (ShortBuffer) frame.samples[0];
                          channelSamplesShortBuffer.rewind();
    
                          ByteBuffer outBuffer = ByteBuffer.allocate(channelSamplesShortBuffer.capacity() * 2);
    
                          for (int i = 0; i < channelSamplesShortBuffer.capacity(); i++) {
                              short val = channelSamplesShortBuffer.get(i);
                              outBuffer.putShort(val);
                          }
    
                          AudioFileFormat audiofileFormat = new AudioFileFormat(null, null, typeID);
                          AudioFormat audioFormat = new AudioFormat(44100, 16, 2, true, true);
                          //System.out.println(grabber.getSampleFormat());
                          // Encrypt data and store as the current audio of byte[] type
                          currentAudioFrame = ciphers[curIndex].doFinal(outBuffer.array());
    
                          DISPLAY.updateAudio(outBuffer.array(), grabber.getFormat()); // Display image audio
                      //  Thread.sleep((long) (1000 / grabber.getSampleRate()));
    
                           //                         AudioFormat audioFormat = new AudioFormat(grabber.getSampleRate(), grabber.getAudioBitrate(),
    

    grabber.getAudioChannels(), true, true);
    // DISPLAY.updateAudio(outBuffer.array(), audioFormat); // Display image audio outBuffer.clear();

                          typeID = 2;
                          mediatype = grabber.getFormat();
    
                      }       
                  } else {
                      grabber.restart();
                  } // Restart when reached end of video
              }
              grabber.close();
    
          } catch (IOException e) {
              e.printStackTrace();
    
          } catch (IllegalBlockSizeException e) {
              e.printStackTrace();
    
          } catch (BadPaddingException e) {
              e.printStackTrace();
    
          }       }
    
      public synchronized int getCurKeyIndex() {          return curIndex;        }
    
      public synchronized void getVideoFile(String video_file) {
          videoFile = video_file;             grabber = new
    

    FFmpegFrameGrabber(video_file);

          try {
              grabber.release();
              grabber.restart();
    
          } catch (FrameGrabber.Exception e) {
              e.printStackTrace();            }       }   }
    

    public void setVideoFile(String videoFile) { this.videoFile = videoFile; }

    public void setThreadFile(String video_file) { fileStreamingThread.getVideoFile(video_file); videoStreamingThread.getVideoFile(video_file); }

    private class BroadcastThread extends Thread { public void run() { while (isRunning) { Thread.yield();

              for (int i = 0; i < sHandlers.size(); i++) {
                  if (sHandlers.get(i).getClientState() == ClientState.R) {
                      sHandlers.get(i).setClientState(ClientState.W);
                      BroadcastWorker workerThread = new BroadcastWorker(sHandlers.get(i));
                      workerThread.start();
                  }
              }           }       }   }
    

    private class BroadcastWorker extends Thread { SHandler sHandler = null;

      public BroadcastWorker(SHandler sHandler) {             this.sHandler =
    

    sHandler; }

      public void run() {             try {
              DatagramSocket out = new DatagramSocket(); // used to send UDP packets
    
              while (sHandler.getClientState() == ClientState.W) {
                  Thread.yield();
    
                  StreamFile s = new StreamFile(typeID, currentVideoFrame, currentAudioFrame, mediatype);
                  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                  ObjectOutputStream os = new ObjectOutputStream(outputStream);
                  os.writeObject(s);
                  byte[] data = outputStream.toByteArray();
                      // Create and send UDP packet
                  DatagramPacket videoPacket = new DatagramPacket(data, data.length,
                          sHandler.getClientSocket().getInetAddress(),
                          Integer.parseInt(sHandler.getClientPort()));
                      out.send(videoPacket);
    
              }           } catch (IOException e) {
              e.printStackTrace();            }       }   }
    

    private class ConnectThread extends Thread { public void run() { TEXT_AREA.append("\nWaiting for clients' connection.....\n");

          try {
              ServerSocket serverSocket = new ServerSocket(PORT);
              Socket clientSocket = null;
    
              while (isRunning) {
                  clientSocket = serverSocket.accept();
    
                  if (isRunning) {
                      SHandler sHandler = new SHandler(clientSocket, serverRSA, clientRSA, sessionKeys[keyIndex],
                              TEXT_AREA);
                      sHandler.start();
                      sHandlers.add(sHandler);
                  }
              }
              serverSocket.close();
              if (clientSocket != null) {
                  clientSocket.close();
              }
    
          } catch (IOException e) {
              e.printStackTrace();            }       }   } }
    

    my audio and image not sync.

  • FFmpeg tile cropping

    24 janvier 2019, par MSH

    This image is splitted to 12 equal pieces. Is there any way to do such a work with ffmpeg?

    this images was uploaded on Instagram

  • How to tell if a video is worthwhile converting to HEVC/H.265 ? [on hold]

    24 janvier 2019, par Dean

    I'm encoding videos to HEVC to save space on a drive.

    I'd like to check if video conversion to HEVC is worthwhile before a conversion attempt (HDD space wise).

    Being able to tell if a file will benefit from conversion to HEVC would be useful.

    Can ffprobe assess if it would be worthwhile?

  • Local ffmpeg output to S3 Bucket

    24 janvier 2019, par eric

    Heres my setup; - I have a local PC running ffmpeg with output configured to h.264 and aac - and S3 bucket created at AWS

    what i need to do is, use ffmpeg [local] output to upload files directly to s3 bucket. PS: Planing to use that s3 bucket with cloudfront to allow 1 [one] user to stream a live event with about setup.

    i could not find a way to specify output location as s3 bucket [with key]. any ideas as to how to do it? Thanks