Skip to content

Basic Video Conversion

Convert video files from one format to another using various tools.

VideoAlchemy Compose File

version: 1

generate_path: "./generated"

tasks:
  - name: Basic Video Conversion
    command: ffmpeg
    inputs:
      - id: input_1
        source: 'input.mp4'
    outputs:
      - id: output_avi
        overwrite: true
        source: 'output.avi'

FFmpeg Command

ffmpeg -i input.mp4 output.avi

Parameters for FFmpeg

  • -i input.mp4: Specifies the input file. Replace input.mp4 with the path to your source video file.
  • output.avi: Specifies the output file. The extension of the output file determines the output format. Replace output.avi with your desired output file name and format.

Possible Errors for FFmpeg

  • File not found: Occurs if FFmpeg cannot locate the input file. Ensure the path to the file is correct.
  • Unsupported codec: Occurs if the output format requires a codec not supported or not installed. Ensure that the desired output format is supported.
  • Permission denied: Occurs if FFmpeg does not have the necessary permissions to read the input file or write to the output file. Ensure that the files and directories have the correct permissions.

GPU Acceleration Command for FFmpeg

For Nvidia GPUs, use:

ffmpeg -hwaccel cuda -i input.mp4 -c:v h264_nvenc output.avi

GStreamer Command

gst-launch-1.0 filesrc location=input.mp4 ! decodebin ! videoconvert ! x264enc ! avimux ! filesink location=output.avi

Parameters for GStreamer

  • filesrc location=input.mp4: Specifies the source file.
  • videoconvert: Converts video formats for compatibility.
  • x264enc: Uses the x264 encoder for H.264 video encoding.
  • avimux: Muxes streams into an AVI container.
  • filesink location=output.avi: Specifies the destination file.

Possible Errors for GStreamer

  • Pipeline configuration error: Occurs if there's an issue with how the pipeline is set up or if a necessary plugin is missing.
  • Unsupported codec or format: Similar to FFmpeg, if the pipeline uses codecs or formats not supported by GStreamer or not installed on the system.
  • File read/write error: If GStreamer cannot access the source or destination files due to permissions or if the destination path is incorrect.

OpenCV Command

Using OpenCV for video conversion requires writing a small script. Here's an example in Python:

import cv2

# Load the input video
cap = cv2.VideoCapture('input.mp4')

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if not ret:
        break

    # Write the frame into the file 'output.avi'
    out.write(frame)

# Release everything if job is finished
cap.release()
out.release()

Possible Errors for OpenCV

  • Codec not supported: If the specified codec is not available on the system, OpenCV will fail to write the video file.
  • Incorrect file path: If the path to the input file or the destination for the output file is incorrect, OpenCV will not be able to open or save the file.
  • Frame capture error: During video processing, if OpenCV cannot read frames from the video, it might stop the conversion process prematurely.

libVLC Command

Converting video files with libVLC also typically involves scripting. Below is an example command using the VLC command line:

vlc -I dummy input.mp4 vlc://quit --sout "#transcode{vcodec=h264,vb=800,acodec=mp3,ab=128}:standard{access=file,mux=avi,dst='output.avi'}"

Parameters for libVLC

  • -I dummy: Runs VLC without an interface.
  • input.mp4: Specifies the input file.
  • --sout: Defines the stream output chain.
  • vcodec=h264,vb=800: Sets the video codec to H.264 with a bitrate of 800 kb/s.
  • acodec=mp3,ab=128: Sets the audio codec to MP3 with a bitrate of 128 kb/s.
  • mux=avi: Uses AVI as the muxing format.
  • dst='output.avi': Specifies the destination file.

Possible Errors for libVLC

  • Unsupported codec or format: If VLC does not support the specified codec or format for the input or output files.
  • Streaming error: When using VLC for streaming or converting streaming media, network issues or incorrect stream URLs can cause errors.
  • Permission issues: Similar to other tools, if VLC lacks permissions to access the input file or write to the output location, it can result in errors.

Additional Information

FFmpeg

  • Codecs and Formats: The output format is determined by the file extension of the output file (e.g., .mp4, .avi, .mkv). The codec used for encoding can be specified with the -c:v parameter (e.g., -c:v libx264 for H.264).
  • Quality and Compression: You can control the quality and compression of the output video by adjusting the bitrate (using -b:v) or the constant rate factor (using -crf for codecs like H.264).
  • Audio: By default, FFmpeg will also transcode the audio stream. You can specify the audio codec with -c:a (e.g., -c:a aac for AAC audio) and adjust the audio bitrate with -b:a.
  • Compatibility: Ensure that the chosen codecs and formats are compatible with your intended playback devices or platforms.

GStreamer

  • Codecs and Formats: Uses a pipeline-based approach for handling various codecs and formats. The encoding process is determined by the elements in the pipeline (e.g., x264enc for H.264 encoding).
  • Quality and Compression: Allows for dynamic adjustment of quality and compression settings within the pipeline. Parameters like bitrate can be specified for certain encoders.
  • Audio: Supports audio processing and encoding through its pipeline. Audio codecs and parameters can be specified similar to video settings.
  • Compatibility: Modular architecture enables support for a wide range of codecs and formats, ensuring compatibility across different platforms and devices.

OpenCV

  • Codecs and Formats: Limited by system codecs but supports common formats for reading and writing videos. The codec is specified in the VideoWriter constructor (e.g., cv2.VideoWriter_fourcc(*'DIVX') for AVI).
  • Quality and Compression: Offers control over the video's resolution and frame rate, which can indirectly affect quality and size.
  • Audio: Does not natively support audio processing; OpenCV focuses primarily on video and image processing.
  • Compatibility: Works well for a wide range of video processing tasks, but compatibility with codecs and formats is dependent on the underlying system.

libVLC

  • Codecs and Formats: Supports a broad range of codecs and formats for both input and output, leveraging VLC's extensive format support.
  • Quality and Compression: Quality and compression settings can be adjusted through transcode module options, like specifying video bitrate and scale.
  • Audio: Offers comprehensive audio processing capabilities, including codec selection and bitrate adjustments, similar to its video processing features.
  • Compatibility: Highly compatible with numerous devices and platforms, benefiting from VLC's universal media playback capabilities.