Recherche avancée

Médias (0)

Mot : - Tags -/formulaire

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (43)

  • Des sites réalisés avec MediaSPIP

    2 mai 2011, par

    Cette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
    Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page.

  • Configurer la prise en compte des langues

    15 novembre 2010, par

    Accéder à la configuration et ajouter des langues prises en compte
    Afin de configurer la prise en compte de nouvelles langues, il est nécessaire de se rendre dans la partie "Administrer" du site.
    De là, dans le menu de navigation, vous pouvez accéder à une partie "Gestion des langues" permettant d’activer la prise en compte de nouvelles langues.
    Chaque nouvelle langue ajoutée reste désactivable tant qu’aucun objet n’est créé dans cette langue. Dans ce cas, elle devient grisée dans la configuration et (...)

  • Les autorisations surchargées par les plugins

    27 avril 2010, par

    Mediaspip core
    autoriser_auteur_modifier() afin que les visiteurs soient capables de modifier leurs informations sur la page d’auteurs

Sur d’autres sites (8471)

  • How to stream synchronized video and audio in real-time from an Android smartphone using HLS while preserving orientation metadata ?

    6 mars, par Jérôme LAROSE
    Hello,  
I am working on an Android application where I need to stream video
from one or two cameras on my smartphone, along with audio from the
microphone, in real-time via a link or web page accessible to users.
The stream should be live, allow rewinding (DVR functionality), and be
recorded simultaneously. A latency of 1 to 2 minutes is acceptable,
and the streaming is one-way.  

I have chosen HLS (HTTP Live Streaming) for its browser compatibility
and DVR support. However, I am encountering issues with audio-video
synchronization, managing camera orientation metadata, and format
conversions.


    


    Here are my attempts :

    


      

    1. MP4 segmentation with MediaRecorder

      


        

      • I used MediaRecorder with setNextOutputFile to generate short MP4 segments, then ffmpeg-kit to convert them to fMP4 for HLS.
      • 


      • Expected : Well-aligned segments for smooth HLS playback.
      • 


      • Result : Timestamp issues causing jumps or interruptions in playback.
      • 


      


    2. 


    3. MPEG2-TS via local socket

      


        

      • I configured MediaRecorder to produce an MPEG2-TS stream sent via a local socket to ffmpeg-kit.
      • 


      • Expected : Stable streaming with preserved metadata.
      • 


      • Result : Streaming works, but orientation metadata is lost, leading to incorrectly oriented video (e.g., rotated 90°).
      • 


      


    4. 


    5. Orientation correction with ffmpeg

      


        

      • I tested -vf transpose=1 in ffmpeg to correct the orientation.
      • 


      • Expected : Correctly oriented video without excessive latency.
      • 


      • Result : Re-encoding takes too long for real-time streaming, causing unacceptable latency.
      • 


      


    6. 


    7. MPEG2-TS to fMP4 conversion

      


        

      • I converted the MPEG2-TS stream to fMP4 with ffmpeg to preserve orientation.
      • 


      • Expected : Perfect audio-video synchronization.
      • 


      • Result : Slight desynchronization between audio and video, affecting the user experience.
      • 


      


    8. 


    


    I am looking for a solution to :

    


      

    • Stream an HLS feed from Android with correctly timestamped segments.
    • 


    • Preserve orientation metadata without heavy re-encoding.
    • 


    • Ensure perfect audio-video synchronization.
    • 


    


    UPDATE

    


    package com.example.angegardien

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.graphics.SurfaceTexture
import android.hardware.camera2.*
import android.media.*
import android.os.*
import android.util.Log
import android.view.Surface
import android.view.TextureView
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.core.app.ActivityCompat
import com.arthenica.ffmpegkit.FFmpegKit
import fi.iki.elonen.NanoHTTPD
import kotlinx.coroutines.*
import java.io.File
import java.io.IOException
import java.net.ServerSocket
import android.view.OrientationEventListener

/**
 * MainActivity class:
 * - Manages camera operations using the Camera2 API.
 * - Records video using MediaRecorder.
 * - Pipes data to FFmpeg to generate HLS segments.
 * - Hosts a local HLS server using NanoHTTPD to serve the generated HLS content.
 */
class MainActivity : ComponentActivity() {

    // TextureView used for displaying the camera preview.
    private lateinit var textureView: TextureView
    // Camera device instance.
    private lateinit var cameraDevice: CameraDevice
    // Camera capture session for managing capture requests.
    private lateinit var cameraCaptureSession: CameraCaptureSession
    // CameraManager to access camera devices.
    private lateinit var cameraManager: CameraManager
    // Directory where HLS output files will be stored.
    private lateinit var hlsDir: File
    // Instance of the HLS server.
    private lateinit var hlsServer: HlsServer

    // Camera id ("1" corresponds to the rear camera).
    private val cameraId = "1"
    // Flag indicating whether recording is currently active.
    private var isRecording = false

    // MediaRecorder used for capturing audio and video.
    private lateinit var activeRecorder: MediaRecorder
    // Surface for the camera preview.
    private lateinit var previewSurface: Surface
    // Surface provided by MediaRecorder for recording.
    private lateinit var recorderSurface: Surface

    // Port for the FFmpeg local socket connection.
    private val ffmpegPort = 8080

    // Coroutine scope to manage asynchronous tasks.
    private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())

    // Variables to track current device rotation and listen for orientation changes.
    private var currentRotation = 0
    private lateinit var orientationListener: OrientationEventListener

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Initialize the TextureView and set it as the content view.
        textureView = TextureView(this)
        setContentView(textureView)

        // Get the CameraManager system service.
        cameraManager = getSystemService(CAMERA_SERVICE) as CameraManager
        // Setup the directory for HLS output.
        setupHLSDirectory()

        // Start the local HLS server on port 8081.
        hlsServer = HlsServer(8081, hlsDir, this)
        try {
            hlsServer.start()
            Log.d("HLS_SERVER", "HLS Server started on port 8081")
        } catch (e: IOException) {
            Log.e("HLS_SERVER", "Error starting HLS Server", e)
        }

        // Initialize the current rotation.
        currentRotation = getDeviceRotation()

        // Add a listener to detect orientation changes.
        orientationListener = object : OrientationEventListener(this) {
            override fun onOrientationChanged(orientation: Int) {
                if (orientation == ORIENTATION_UNKNOWN) return // Skip unknown orientations.
                // Determine the new rotation angle.
                val newRotation = when {
                    orientation >= 315 || orientation < 45 -> 0
                    orientation >= 45 && orientation < 135 -> 90
                    orientation >= 135 && orientation < 225 -> 180
                    orientation >= 225 && orientation < 315 -> 270
                    else -> 0
                }
                // If the rotation has changed and recording is active, update the rotation.
                if (newRotation != currentRotation && isRecording) {
                    Log.d("ROTATION", "Orientation change detected: $newRotation")
                    currentRotation = newRotation
                }
            }
        }
        orientationListener.enable()

        // Set up the TextureView listener to know when the surface is available.
        textureView.surfaceTextureListener = object : TextureView.SurfaceTextureListener {
            override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
                // Open the camera when the texture becomes available.
                openCamera()
            }
            override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture) = false
            override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}
        }
    }

    /**
     * Sets up the HLS directory in the public Downloads folder.
     * If the directory exists, it deletes it recursively and creates a new one.
     */
    private fun setupHLSDirectory() {
        val downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
        hlsDir = File(downloadsDir, "HLS_Output")

        if (hlsDir.exists()) {
            hlsDir.deleteRecursively()
        }
        hlsDir.mkdirs()

        Log.d("HLS", "📂 HLS folder created: ${hlsDir.absolutePath}")
    }

    /**
     * Opens the camera after checking for necessary permissions.
     */
    private fun openCamera() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
            ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            // Request permissions if they are not already granted.
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO), 101)
            return
        }

        try {
            // Open the specified camera using its cameraId.
            cameraManager.openCamera(cameraId, object : CameraDevice.StateCallback() {
                override fun onOpened(camera: CameraDevice) {
                    cameraDevice = camera
                    // Start the recording session once the camera is opened.
                    startNextRecording()
                }
                override fun onDisconnected(camera: CameraDevice) { camera.close() }
                override fun onError(camera: CameraDevice, error: Int) { camera.close() }
            }, null)
        } catch (e: CameraAccessException) {
            e.printStackTrace()
        }
    }

    /**
     * Starts a new recording session:
     * - Sets up the preview and recorder surfaces.
     * - Creates a pipe for MediaRecorder output.
     * - Creates a capture session for simultaneous preview and recording.
     */
    private fun startNextRecording() {
        // Get the SurfaceTexture from the TextureView and set its default buffer size.
        val texture = textureView.surfaceTexture!!
        texture.setDefaultBufferSize(1920, 1080)
        // Create the preview surface.
        previewSurface = Surface(texture)

        // Create and configure the MediaRecorder.
        activeRecorder = createMediaRecorder()

        // Create a pipe to route MediaRecorder data.
        val pipe = ParcelFileDescriptor.createPipe()
        val pfdWrite = pipe[1] // Write end used by MediaRecorder.
        val pfdRead = pipe[0]  // Read end used by the local socket server.

        // Set MediaRecorder output to the file descriptor of the write end.
        activeRecorder.setOutputFile(pfdWrite.fileDescriptor)
        setupMediaRecorder(activeRecorder)
        // Obtain the recorder surface from MediaRecorder.
        recorderSurface = activeRecorder.surface

        // Create a capture request using the RECORD template.
        val captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD)
        captureRequestBuilder.addTarget(previewSurface)
        captureRequestBuilder.addTarget(recorderSurface)

        // Create a capture session including both preview and recorder surfaces.
        cameraDevice.createCaptureSession(
            listOf(previewSurface, recorderSurface),
            object : CameraCaptureSession.StateCallback() {
                override fun onConfigured(session: CameraCaptureSession) {
                    cameraCaptureSession = session
                    captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO)
                    // Start a continuous capture request.
                    cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null)

                    // Launch a coroutine to start FFmpeg and MediaRecorder with synchronization.
                    scope.launch {
                        startFFmpeg()
                        delay(500) // Wait for FFmpeg to be ready.
                        activeRecorder.start()
                        isRecording = true
                        Log.d("HLS", "🎥 Recording started...")
                    }

                    // Launch a coroutine to run the local socket server to forward data.
                    scope.launch {
                        startLocalSocketServer(pfdRead)
                    }
                }
                override fun onConfigureFailed(session: CameraCaptureSession) {
                    Log.e("Camera2", "❌ Configuration failed")
                }
            },
            null
        )
    }

    /**
     * Coroutine to start a local socket server.
     * It reads from the MediaRecorder pipe and sends the data to FFmpeg.
     */
    private suspend fun startLocalSocketServer(pfdRead: ParcelFileDescriptor) {
        withContext(Dispatchers.IO) {
            val serverSocket = ServerSocket(ffmpegPort)
            Log.d("HLS", "Local socket server started on port $ffmpegPort")

            // Accept connection from FFmpeg.
            val socket = serverSocket.accept()
            Log.d("HLS", "Connection accepted from FFmpeg")

            // Read data from the pipe and forward it through the socket.
            val inputStream = ParcelFileDescriptor.AutoCloseInputStream(pfdRead)
            val outputStream = socket.getOutputStream()
            val buffer = ByteArray(8192)
            var bytesRead: Int
            while (inputStream.read(buffer).also { bytesRead = it } != -1) {
                outputStream.write(buffer, 0, bytesRead)
            }
            outputStream.close()
            inputStream.close()
            socket.close()
            serverSocket.close()
        }
    }

    /**
     * Coroutine to start FFmpeg using a local TCP input.
     * Applies a video rotation filter based on device orientation and generates HLS segments.
     */
    private suspend fun startFFmpeg() {
        withContext(Dispatchers.IO) {
            // Retrieve the appropriate transpose filter based on current rotation.
            val transposeFilter = getTransposeFilter(currentRotation)

            // FFmpeg command to read from the TCP socket and generate an HLS stream.
            // Two alternative commands are commented below.
            // val ffmpegCommand = "-fflags +genpts -i tcp://localhost:$ffmpegPort -c copy -bsf:a aac_adtstoasc -movflags +faststart -f dash -seg_duration 10 -hls_playlist 1 ${hlsDir.absolutePath}/manifest.mpd"
            // val ffmpegCommand = "-fflags +genpts -i tcp://localhost:$ffmpegPort -c copy -bsf:a aac_adtstoasc -movflags +faststart -f hls -hls_time 5 -hls_segment_type fmp4 -hls_flags split_by_time -hls_list_size 0 -hls_playlist_type event -hls_fmp4_init_filename init.mp4 -hls_segment_filename ${hlsDir.absolutePath}/segment_%03d.m4s ${hlsDir.absolutePath}/playlist.m3u8"
            val ffmpegCommand = "-fflags +genpts -i tcp://localhost:$ffmpegPort -vf $transposeFilter -c:v libx264 -preset ultrafast -crf 23 -c:a copy -movflags +faststart -f hls -hls_time 0.1 -hls_segment_type mpegts -hls_flags split_by_time -hls_list_size 0 -hls_playlist_type event -hls_segment_filename ${hlsDir.absolutePath}/segment_%03d.ts ${hlsDir.absolutePath}/playlist.m3u8"

            FFmpegKit.executeAsync(ffmpegCommand) { session ->
                if (session.returnCode.isValueSuccess) {
                    Log.d("HLS", "✅ HLS generated successfully")
                } else {
                    Log.e("FFmpeg", "❌ Error generating HLS: ${session.allLogsAsString}")
                }
            }
        }
    }

    /**
     * Gets the current device rotation using the WindowManager.
     */
    private fun getDeviceRotation(): Int {
        val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        return when (windowManager.defaultDisplay.rotation) {
            Surface.ROTATION_0 -> 0
            Surface.ROTATION_90 -> 90
            Surface.ROTATION_180 -> 180
            Surface.ROTATION_270 -> 270
            else -> 0
        }
    }

    /**
     * Returns the FFmpeg transpose filter based on the rotation angle.
     * Used to rotate the video stream accordingly.
     */
    private fun getTransposeFilter(rotation: Int): String {
        return when (rotation) {
            90 -> "transpose=1" // 90° clockwise
            180 -> "transpose=2,transpose=2" // 180° rotation
            270 -> "transpose=2" // 90° counter-clockwise
            else -> "transpose=0" // No rotation
        }
    }

    /**
     * Creates and configures a MediaRecorder instance.
     * Sets up audio and video sources, formats, encoders, and bitrates.
     */
    private fun createMediaRecorder(): MediaRecorder {
        return MediaRecorder().apply {
            setAudioSource(MediaRecorder.AudioSource.MIC)
            setVideoSource(MediaRecorder.VideoSource.SURFACE)
            setOutputFormat(MediaRecorder.OutputFormat.MPEG_2_TS)
            setVideoEncodingBitRate(5000000)
            setVideoFrameRate(24)
            setVideoSize(1080, 720)
            setVideoEncoder(MediaRecorder.VideoEncoder.H264)
            setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
            setAudioSamplingRate(16000)
            setAudioEncodingBitRate(96000) // 96 kbps
        }
    }

    /**
     * Prepares the MediaRecorder and logs the outcome.
     */
    private fun setupMediaRecorder(recorder: MediaRecorder) {
        try {
            recorder.prepare()
            Log.d("HLS", "✅ MediaRecorder prepared")
        } catch (e: IOException) {
            Log.e("HLS", "❌ Error preparing MediaRecorder", e)
        }
    }

    /**
     * Custom HLS server class extending NanoHTTPD.
     * Serves HLS segments and playlists from the designated HLS directory.
     */
    private inner class HlsServer(port: Int, private val hlsDir: File, private val context: Context) : NanoHTTPD(port) {
        override fun serve(session: IHTTPSession): Response {
            val uri = session.uri.trimStart('/')

            // Intercept the request for `init.mp4` and serve it from assets.
            /*
            if (uri == "init.mp4") {
                Log.d("HLS Server", "📡 Intercepting init.mp4, sending file from assets...")
                return try {
                    val assetManager = context.assets
                    val inputStream = assetManager.open("init.mp4")
                    newFixedLengthResponse(Response.Status.OK, "video/mp4", inputStream, inputStream.available().toLong())
                } catch (e: Exception) {
                    Log.e("HLS Server", "❌ Error reading init.mp4 from assets: ${e.message}")
                    newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Server error")
                }
            }
            */

            // Serve all other HLS files normally from the hlsDir.
            val file = File(hlsDir, uri)
            return if (file.exists()) {
                newFixedLengthResponse(Response.Status.OK, getMimeTypeForFile(uri), file.inputStream(), file.length())
            } else {
                newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "File not found")
            }
        }
    }

    /**
     * Clean up resources when the activity is destroyed.
     * Stops recording, releases the camera, cancels coroutines, and stops the HLS server.
     */
    override fun onDestroy() {
        super.onDestroy()
        if (isRecording) {
            activeRecorder.stop()
            activeRecorder.release()
        }
        cameraDevice.close()
        scope.cancel()
        hlsServer.stop()
        orientationListener.disable()
        Log.d("HLS", "🛑 Activity destroyed")
    }
}


    


    I have three examples of ffmpeg commands.

    


      

    • One command segments into DASH, but the camera does not have the correct rotation.
    • 


    • One command segments into HLS without re-encoding with 5-second segments ; it’s fast but does not have the correct rotation.
    • 


    • One command segments into HLS with re-encoding, which applies a rotation. It’s too slow for 5-second segments, so a 1-second segment was chosen.
    • 


    


    Note :

    


      

    • In the second command ("One command segments into HLS without re-encoding with 5-second segments ; it’s fast but does not have the correct rotation."), it returns fMP4. To achieve the correct rotation, I provide a preconfigured init.mp4 file during the HTTP request to retrieve it (see comment).
    • 


    • In the third command ("One command segments into HLS with re-encoding, which applies a rotation. It’s too slow for 5-second segments, so a 1-second segment was chosen."), it returns TS.
    • 


    


  • Facebook Reels Upload always failing

    21 juin, par Evrard A.

    I'm trying to upload Reels through Facebook Graph API. The video is created with the following ffmpeg command.

    


    cmd = [
            'ffmpeg',
            '-i', video_path,
            '-i', voice_path,
            '-i', music_path,

            '-filter_complex',
            '[1:a]loudnorm=I=-16:LRA=11:TP=-1.5,adelay=0|0[a1];' +
            '[2:a]volume=0.2,afade=t=in:ss=0:d=0.02,afade=t=out:st=28:d=0.03[a2];' +
            '[a1][a2]amix=inputs=2:duration=first:dropout_transition=0[aout]',

            '-map', '0:v:0',
            '-map', '[aout]',

            '-vf',
             f"subtitles='{str(ass_path)}',format=yuv420p,scale=1080:1920:force_original_aspect_ratio=decrease,pad=1080:1920:(ow-iw)/2:(oh-ih)/2,setsar=1",  # Incrustation des sous-titres

            '-r', '30',
            '-g', '60',
            '-keyint_min', '60',
            '-sc_threshold', '0',
            '-x264opts', 'no-scenecut',

            '-c:v', 'libx264',
            '-profile:v', 'baseline',
            '-level', '4.1',
            '-pix_fmt', 'yuv420p',
            '-color_range', 'tv',
            '-colorspace', 'bt709',

            '-b:v', '9500k',
            '-maxrate', '9500k',
            '-bufsize', '19000k',

            '-c:a', 'aac',
            '-b:a', '192k',
            '-ac', '2',
            '-ar', '48000',

            '-movflags', '+faststart',
            '-video_track_timescale', '15360',
            '-max_muxing_queue_size', '9999',

            '-y', self.output_video_path if self.output_video_path else f'{parts[0]}.subtitled.{parts[1]}'
        ]

        subprocess.run(cmd, check=True)


    


    Here is the class method I use to publish :

    


      import requests, os,  time
  from datetime import datetime, timedelta
  from moviepy.editor import VideoFileClip
   
  def post_reel(
      self,
      page_id: str,
      page_access_token: str,
      video_file_path: str,
      video_description: str,
      tags: list = None, # type: ignore
      publish_now: bool = True
  ):
      def extract_first_frame(video_path: str, output_image_path: str, time_in_seconds: float = 1):
          """
          Extrait une frame à time_in_seconds et la sauvegarde comme miniature.
          """
          try:
              clip = VideoFileClip(video_path)
              clip.save_frame(output_image_path, t=time_in_seconds)
              print(f"[THUMBNAIL] Frame at {time_in_seconds}s saved to {output_image_path}")
              return output_image_path
          except Exception as e:
              print(f"[ERROR] Could not extract thumbnail: {str(e)}")
              return None

      def wait_for_video_ready(video_id, page_access_token, timeout=300, poll_interval=10):
          """
          Attends que la vidéo soit complètement traitée et publiée.
          """
          status_url = f"{self.BASE_API_URL}/{video_id}"
          params = {
              "access_token": page_access_token,
              "fields": "status"
          }

          start = time.time()
          while time.time() - start < timeout:
              try:
                  r = requests.get(url=status_url, params=params)
                  r.raise_for_status()
                  status = r.json().get("status", {})
                  processing = status.get("processing_phase", {}).get("status")
                  publishing = status.get("publishing_phase", {}).get("status")
                  video_status = status.get("video_status")

                  print(f"[WAIT] video_status={video_status}, processing={processing}, publishing={publishing}")

                  if processing == "complete" and publishing == "complete":
                      print("[READY] Reel processed and published")
                      return True
                  elif processing == "error":
                     print(r.json())

              except Exception as e:
                  print(f"[ERROR] during polling: {e}")

              time.sleep(poll_interval)

          print("[TIMEOUT] Video did not finish processing in time.")
          return False

      try:
          # Step 1: Initialize upload
          init_url = f"{self.BASE_API_URL}/{page_id}/video_reels"
          init_params = {"upload_phase": "start"}
          init_payload = {'access_token': page_access_token}

          r = requests.post(url=init_url, data=init_payload, params=init_params)
          r.raise_for_status()
          response = r.json()
          video_id = response["video_id"]
          upload_url = response["upload_url"]
          print(f"[INIT OK] Video ID: {video_id}")

          # Step 2: Upload video
          file_size = os.path.getsize(video_file_path)
          headers = {
              'Authorization': f"OAuth {page_access_token}",
              'offset': "0",
              'file_size': str(file_size),
          }

          with open(video_file_path, 'rb') as f:
              files = {'source': f}
              r = requests.post(url=upload_url, data=files, headers=headers)
              r.raise_for_status()
              upload_response = r.json()

          if not upload_response.get("success"):
              print("[ERROR] Upload failed.")
              return None
          print(f"[UPLOAD OK]")

          # Step 3: Check video status
          status_check_url = f'{self.BASE_API_URL}/{video_id}'
          check_params = {
              "access_token": page_access_token,
              "fields": "status"
          }
          r = requests.get(url=status_check_url, params=check_params)
          r.raise_for_status()
          print(f"[STATUS CHECK] {r.json()}")

          # Step 4: Finalize video
          finalize_params = {
              "video_id": video_id,
              "upload_phase": "finish",
              "published": "true",
              "access_token": page_access_token,
              "video_state": "PUBLISHED" if publish_now else "SCHEDULED",
              "title": video_description,
              "description": video_description
          }

          if not publish_now:
              finalize_params["scheduled_publish_time"] = int((datetime.now() + timedelta(days=1)).timestamp())

          if tags:
              finalize_params["tags"] = ",".join(tags)

          r = requests.post(url=init_url, params=finalize_params, headers=headers)
          r.raise_for_status()
          finalize_response = r.json()
          post_id = finalize_response.get("post_id")
          print(f"[FINALIZE OK] Post ID: {post_id}")
          
          # WAIT UNTIL PUBLISHED
          if not wait_for_video_ready(video_id, page_access_token):
              print("[ERROR] Reel processing timeout or failure")
              return None
          
          # Step 5: Extract and upload thumbnail
          thumbnail_path = f"temp_thumb_{video_id}.jpg"
          if extract_first_frame(video_file_path, thumbnail_path):
              thumb_url = f"{self.BASE_API_URL}/{video_id}/thumbnails"
              with open(thumbnail_path, 'rb') as img:
                  files = {'source': img}
                  thumb_payload = {'access_token': page_access_token}
                  r = requests.post(url=thumb_url, files=files, data=thumb_payload)
                  r.raise_for_status()
                  print("[THUMBNAIL UPLOADED]")
              # Clean up temp file
              os.remove(thumbnail_path)
              print("[THUMBNAIL CLEANED UP]")

          return post_id

      except Exception as e:
          print(f"[ERROR] {str(e)}")
          return None


    


    Here are the logs I get :

    


      

    • [INIT OK] Video ID: 1020853163558419
    • 


    • [UPLOAD OK]
    • 


    • [STATUS CHECK]
    • 


    


    {
  "status": {
    "video_status": "upload_complete",
    "uploading_phase": {
      "status": "complete",
      "bytes_transferred": 37780189
    },
    "processing_phase": {
      "status": "not_started"
    },
    "publishing_phase": {
      "status": "not_started"
    },
    "copyright_check_status": {
      "status": "in_progress"
    }
  },
  "id": "1020853163558419"
}


    


      

    • [FINALIZE OK] Post ID: 122162302376476425
    • 


    • [WAIT] video_status=upload_complete, processing=not_started, publishing=not_started
    • 


    • [WAIT] video_status=error, processing=error, publishing=not_started
    • 


    


    {
  "status": {
    "video_status": "error",
    "uploading_phase": {
      "status": "complete",
      "bytes_transferred": 37780189
    },
    "processing_phase": {
      "status": "error",
      "errors": [
        {
          "code": 1363008,
          "message": "Video Creation failed, please try again."
        }
      ]
    },
    "publishing_phase": {
      "status": "not_started"
    },
    "copyright_check_status": {
      "status": "in_progress"
    }
  },
  "id": "1020853163558419"
}


    


    It seems the error code 1363008 is related to the video properties format but even after following Facebook Reels video format recommandations, I can't make it work.

    


    Can you help me with this please ?

    


    I failed getting usefull help with ChatGPT 😅, and thanks in advance for anyone who answers or comments my question.

    


  • How to extract frames at 30 fps using FFMPEG APIs on Android ?

    8 septembre 2016, par Amber Beriwal

    We are working on a project that consumes FFMPEG library for video frame extraction on Android platform.

    On Windows, we have observed :

    • Using CLI, ffmpeg is capable of extracting frames at 30 fps using command ffmpeg -i input.flv -vf fps=1 out%d.png.
    • Using Xuggler, we are able to extract frames at 30 fps.
    • Using FFMPEG APIs directly in code, we are getting frames at 30 fps.

    But when we use FFMPEG APIs directly on Android (See Hardware Details), we are getting following results :

    • 720p video (1280 x 720) - 16 fps (approx. 60 ms/frame)
    • 1080p video (1920 x 1080) - 7 fps (approx. 140 ms/frame)

    We haven’t tested Xuggler/CLI on Android yet.

    Ideally, we should be able to get the data in constant time (approx. 30 ms/frame).

    How can we get 30 fps on Android ?

    Code being used on Android :

    if (avformat_open_input(&pFormatCtx, pcVideoFile, NULL, NULL)) {
       iError = -1;  //Couldn't open file
    }

    if (!iError) {
       //Retrieve stream information
       if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
           iError = -2; //Couldn't find stream information
    }

    //Find the first video stream
    if (!iError) {

       for (i = 0; i < pFormatCtx->nb_streams; i++) {
           if (AVMEDIA_TYPE_VIDEO
                   == pFormatCtx->streams[i]->codec->codec_type) {
               iFramesInVideo = pFormatCtx->streams[i]->nb_index_entries;
               duration = pFormatCtx->streams[i]->duration;
               begin = pFormatCtx->streams[i]->start_time;
               time_base = (pFormatCtx->streams[i]->time_base.num * 1.0f)
                       / pFormatCtx->streams[i]->time_base.den;

               pCodecCtx = avcodec_alloc_context3(NULL);
               if (!pCodecCtx) {
                   iError = -6;
                   break;
               }

               AVCodecParameters params = { 0 };
               iReturn = avcodec_parameters_from_context(&params,
                       pFormatCtx->streams[i]->codec);
               if (iReturn < 0) {
                   iError = -7;
                   break;
               }

               iReturn = avcodec_parameters_to_context(pCodecCtx, &params);
               if (iReturn < 0) {
                   iError = -7;
                   break;
               }

               //pCodecCtx = pFormatCtx->streams[i]->codec;

               iVideoStreamIndex = i;
               break;
           }
       }
    }

    if (!iError) {
       if (iVideoStreamIndex == -1) {
           iError = -3; // Didn't find a video stream
       }
    }

    if (!iError) {
       // Find the decoder for the video stream
       pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
       if (pCodec == NULL) {
           iError = -4;
       }
    }

    if (!iError) {
       // Open codec
       if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
           iError = -5;
    }

    if (!iError) {
       iNumBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
               pCodecCtx->height, 1);

       // initialize SWS context for software scaling
       sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
               pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
               AV_PIX_FMT_RGB24,
               SWS_BILINEAR,
               NULL,
               NULL,
               NULL);
       if (!sws_ctx) {
           iError = -7;
       }
    }
    clock_gettime(CLOCK_MONOTONIC_RAW, &end);
    delta_us = (end.tv_sec - start.tv_sec) * 1000000
           + (end.tv_nsec - start.tv_nsec) / 1000;
    start = end;
    //LOGI("Starting_Frame_Extraction: %lld", delta_us);
    if (!iError) {
       while (av_read_frame(pFormatCtx, &packet) == 0) {
           // Is this a packet from the video stream?
           if (packet.stream_index == iVideoStreamIndex) {
               pFrame = av_frame_alloc();
               if (NULL == pFrame) {
                   iError = -8;
                   break;
               }

               // Decode video frame
               avcodec_decode_video2(pCodecCtx, pFrame, &iFrameFinished,
                       &packet);
               if (iFrameFinished) {
                   //OUR CODE
               }
               av_frame_free(&pFrame);
               pFrame = NULL;
           }
           av_packet_unref(&packet);
       }
    }