FFmpegfs Fuse Multi Media Filesystem 2.14
Macros | Typedefs | Functions | Variables
ffmpeg_utils.cc File Reference

FFmpegfs utility set implementation. More...

#include <libswscale/swscale.h>
#include "libavutil/ffversion.h"
#include <libavcodec/avcodec.h>
#include "ffmpeg_utils.h"
#include "id3v1tag.h"
#include "ffmpegfs.h"
#include <iostream>
#include <libgen.h>
#include <algorithm>
#include <wordexp.h>
#include <memory>
#include <fstream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <vector>
#include <cstring>
#include <functional>
#include <chrono>
#include <thread>
#include <iconv.h>
#include <chardet.h>
#include <fnmatch.h>
#include <unistd.h>
#include <libswresample/swresample.h>
Include dependency graph for ffmpeg_utils.cc:

Go to the source code of this file.

Macros

#define AV_ERROR_MAX_STRING_SIZE   128
 Max. length of a FFmpeg error string. More...
 
#define PRINT_LIB_INFO(libname, LIBNAME)
 Print info about a FFmpeg library. More...
 
#define STR_VALUE(arg)   #arg
 Convert macro to string. More...
 
#define X(name)   STR_VALUE(name)
 Convert macro to string. More...
 

Typedefs

typedef std::map< const std::string, const FILETYPE, compFILETYPE_MAP
 Map of file type. One entry per supported type. More...
 

Functions

static int is_device (__attribute__((unused)) const AVClass *avclass)
 Check if class is a FMmpeg device. More...
 
static std::string ffmpeg_libinfo (bool lib_exists, __attribute__((unused)) unsigned int version, __attribute__((unused)) const char *cfg, int version_minor, int version_major, int version_micro, const char *libname)
 Get FFmpeg library info. More...
 
const std::string & append_sep (std::string *path)
 Add / to the path if required. More...
 
const std::string & append_filename (std::string *path, const std::string &filename)
 Add filename to path, including / after the path if required. More...
 
const std::string & remove_sep (std::string *path)
 Remove / from the path. More...
 
const std::string & remove_filename (std::string *filepath)
 Remove filename from path. Handy dirname alternative. More...
 
const std::string & remove_path (std::string *filepath)
 Remove path from filename. Handy basename alternative. More...
 
const std::string & remove_ext (std::string *filepath)
 Remove extension from filename. More...
 
bool find_ext (std::string *ext, const std::string &filename)
 Find extension in filename, if existing. More...
 
bool check_ext (const std::string &ext, const std::string &filename)
 Check if filename has a certain extension. The check is case sensitive. More...
 
const std::string & replace_ext (std::string *filepath, const std::string &ext)
 Replace extension in filename, taking into account that there might not be an extension already. More...
 
const std::string & append_ext (std::string *filepath, const std::string &ext)
 Append extension to filename. If ext is the same as. More...
 
char * new_strdup (const std::string &str)
 strdup() variant taking a std::string as input. More...
 
std::string ffmpeg_geterror (int errnum)
 Get FFmpeg error string for errnum. Internally calls av_strerror(). More...
 
int64_t ffmpeg_rescale_q (int64_t ts, const AVRational &timebase_in, const AVRational &timebase_out)
 Convert a FFmpeg time from in timebase to outtime base. More...
 
int64_t ffmpeg_rescale_q_rnd (int64_t ts, const AVRational &timebase_in, const AVRational &timebase_out)
 Convert a FFmpeg time from in timebase to out timebase with rounding. More...
 
const char * get_media_type_string (enum AVMediaType media_type)
 av_get_media_type_string is missing, so we provide our own. More...
 
std::string ffmpeg_libinfo ()
 Get info about the FFmpeg libraries used. More...
 
int show_caps (int device_only)
 Lists all supported codecs and devices. More...
 
const char * get_codec_name (AVCodecID codec_id, bool long_name)
 Safe way to get the codec name. Function never fails, will return "unknown" on error. More...
 
int mktree (const std::string &path, mode_t mode)
 Make directory tree. More...
 
void tempdir (std::string &path)
 Get temporary directory. More...
 
int supports_albumart (FILETYPE filetype)
 Check if file type supports album arts. More...
 
FILETYPE get_filetype (const std::string &desttype)
 Get the FFmpegfs filetype, desttype must be one of FFmpeg's "official" short names for formats. More...
 
std::string get_filetype_text (FILETYPE filetype)
 Convert FILETYPE enum to human readable text. More...
 
FILETYPE get_filetype_from_list (const std::string &desttypelist)
 Get the FFmpegfs filetype, desttypelist must be a comma separated list of FFmpeg's "official" short names for formats. Will return the first match. Same as get_filetype, but accepts a comma separated list. More...
 
void init_id3v1 (ID3v1 *id3v1)
 Initialise ID3v1 tag. More...
 
std::string format_number (int64_t value)
 Format numeric value. More...
 
std::string format_bitrate (BITRATE value)
 Format a bit rate. More...
 
std::string format_samplerate (int value)
 Format a samplerate. More...
 
std::string format_duration (int64_t value, uint32_t fracs)
 Format a time in format HH:MM:SS.fract. More...
 
std::string format_time (time_t value)
 Format a time in format "w d m s". More...
 
std::string format_size (uint64_t value)
 Format size. More...
 
std::string format_size_ex (uint64_t value)
 Format size. More...
 
std::string format_result_size (size_t size_resulting, size_t size_predicted)
 Format size of transcoded file including difference between predicted and resulting size. More...
 
std::string format_result_size_ex (size_t size_resulting, size_t size_predicted)
 Format size of transcoded file including difference between predicted and resulting size. More...
 
static void print_fps (double d, const char *postfix)
 Print frames per second. More...
 
int print_stream_info (const AVStream *stream)
 Print info about an AVStream. More...
 
std::string fourcc_make_string (std::string *buf, uint32_t fourcc)
 
void exepath (std::string *path)
 Path to FFmpegfs binary. More...
 
std::string & ltrim (std::string &s)
 trim from start More...
 
std::string & rtrim (std::string &s)
 trim from end More...
 
std::string & trim (std::string &s)
 trim from both ends More...
 
std::string replace_all (std::string str, const std::string &from, const std::string &to)
 Same as std::string replace(), but replaces all occurrences. More...
 
std::string replace_all (std::string *str, const std::string &from, const std::string &to)
 Same as std::string replace(), but replaces string in-place. More...
 
bool replace_start (std::string *str, const std::string &from, const std::string &to)
 Replace start of string from "from" to "to". More...
 
int strcasecmp (const std::string &s1, const std::string &s2)
 strcasecmp() equivalent for std::string. More...
 
int reg_compare (const std::string &value, const std::string &pattern, std::regex::flag_type flag)
 Compare value with pattern. More...
 
const std::string & expand_path (std::string *tgt, const std::string &src)
 Expand path, e.g., expand ~/ to home directory. More...
 
int is_mount (const std::string &path)
 Check if path is a mount. More...
 
std::vector< std::string > split (const std::string &input, const std::string &regex)
 Split string into an array delimited by a regular expression. More...
 
std::string sanitise_filepath (std::string *filepath)
 Sanitise file name. Calls realpath() to remove duplicate // or resolve ../.. etc. Changes the path in place. More...
 
std::string sanitise_filepath (const std::string &filepath)
 Sanitise file name. Calls realpath() to remove duplicate // or resolve ../.. etc. More...
 
void append_basepath (std::string *origpath, const char *path)
 Translate file names from FUSE to the original absolute path. More...
 
bool is_album_art (AVCodecID codec_id, const AVRational *frame_rate)
 Minimal check if codec is an album art. Requires frame_rate to decide whether this is a video stream if codec_id is not BMP or PNG (which means its undoubtedly an album art). For MJPEG this may also be a video stream if the frame rate is high enough. More...
 
int nocasecompare (const std::string &lhs, const std::string &rhs)
 nocasecompare to make std::string find operations case insensitive More...
 
size_t get_disk_free (std::string &path)
 Get free disk space. More...
 
bool check_ignore (size_t size, size_t offset)
 For use with win_smb_fix=1: Check if this an illegal access offset by Windows. More...
 
std::string make_filename (uint32_t file_no, const std::string &fileext)
 Make a file name from file number and file extension. More...
 
bool file_exists (const std::string &filename)
 Check if file exists. More...
 
void make_upper (std::string *input)
 Convert string to upper case. More...
 
void make_lower (std::string *input)
 Convert string to lower case. More...
 
const char * hwdevice_get_type_name (AVHWDeviceType dev_type)
 
int to_utf8 (std::string &text, const std::string &encoding)
 Convert almost any encoding to UTF-8. To get a list of all possible encodings run "iconv --list". More...
 
int get_encoding (const char *str, std::string &encoding)
 Try to detect the encoding of str. This is relatively realiable, but may be wrong. More...
 
int read_file (const std::string &path, std::string &result)
 Read text file and return in UTF-8 format, no matter in which encoding the input file is. UTF-8/16/32 with BOM will always return a correct result. For all other encodings the function tries to detect it, that may fail. More...
 
void stat_set_size (struct stat *st, size_t size)
 Properly fill in all size related members in stat struct. More...
 
bool detect_docker ()
 Detect if we are running under Docker. More...
 
bool is_text_codec (AVCodecID codec_id)
 Check if subtitle codec is a text or graphical codec. More...
 
int get_audio_props (AVFormatContext *format_ctx, int *channels, int *samplerate)
 Get first audio stream. More...
 
const std::string & regex_escape (std::string *str)
 Escape characters that are meaningful to regexp. More...
 
bool is_selected (const std::string &ext)
 Find extension in include list, if existing. More...
 
bool is_blocked (const std::string &filename)
 Check if filename should be hidden from output path. More...
 
void save_free (void **p)
 Savely free memory: Pointer will be set to nullptr before it is actually freed. More...
 
void mssleep (int milliseconds)
 Sleep for specified time. More...
 
void ussleep (int microseconds)
 Sleep for specified time. More...
 
void nssleep (int nanoseconds)
 Sleep for specified time. More...
 

Variables

static const FILETYPE_MAP filetype_map
 

Detailed Description

FFmpegfs utility set implementation.

Author
Norbert Schlia (nschl.nosp@m.ia@o.nosp@m.blivi.nosp@m.on-s.nosp@m.oftwa.nosp@m.re.d.nosp@m.e)

Definition in file ffmpeg_utils.cc.

Macro Definition Documentation

◆ AV_ERROR_MAX_STRING_SIZE

#define AV_ERROR_MAX_STRING_SIZE   128

Max. length of a FFmpeg error string.

Definition at line 96 of file ffmpeg_utils.cc.

◆ PRINT_LIB_INFO

#define PRINT_LIB_INFO (   libname,
  LIBNAME 
)
Value:
ffmpeg_libinfo(true, libname##_version(), libname##_configuration(), \
LIB##LIBNAME##_VERSION_MAJOR, LIB##LIBNAME##_VERSION_MINOR, LIB##LIBNAME##_VERSION_MICRO, #libname)
static std::string ffmpeg_libinfo(bool lib_exists, __attribute__((unused)) unsigned int version, __attribute__((unused)) const char *cfg, int version_minor, int version_major, int version_micro, const char *libname)
Get FFmpeg library info.

Print info about a FFmpeg library.

Definition at line 1233 of file ffmpeg_utils.cc.

◆ STR_VALUE

#define STR_VALUE (   arg)    #arg

Convert macro to string.

Definition at line 1549 of file ffmpeg_utils.cc.

◆ X

#define X (   name)    STR_VALUE(name)

Convert macro to string.

Definition at line 1550 of file ffmpeg_utils.cc.

Typedef Documentation

◆ FILETYPE_MAP

typedef std::map<const std::string, const FILETYPE, comp> FILETYPE_MAP

Map of file type. One entry per supported type.

Definition at line 99 of file ffmpeg_utils.cc.

Function Documentation

◆ append_basepath()

void append_basepath ( std::string *  origpath,
const char *  path 
)

Translate file names from FUSE to the original absolute path.

Parameters
[out]origpath- Upon return, contains the name and path of the original file.
[in]path- Filename and relative path of the original file.

Definition at line 2000 of file ffmpeg_utils.cc.

Referenced by ffmpegfs_fgetattr(), ffmpegfs_getattr(), ffmpegfs_open(), ffmpegfs_read(), ffmpegfs_readdir(), ffmpegfs_readlink(), and ffmpegfs_statfs().

◆ append_ext()

const std::string & append_ext ( std::string *  filepath,
const std::string &  ext 
)

Append extension to filename. If ext is the same as.

Parameters
[in]filepath- Filename to add extension to.
[in]ext- Extension to add.
Returns
Returns constant reference to filename.

Definition at line 1107 of file ffmpeg_utils.cc.

References strcasecmp().

Referenced by check_cuesheet(), parse_cuesheet(), and virtual_name().

◆ append_filename()

const std::string & append_filename ( std::string *  path,
const std::string &  filename 
)

Add filename to path, including / after the path if required.

Parameters
[in]path- Path to add filename to.
[in]filename- File name to add.
Returns
Returns constant reference to path.

Definition at line 994 of file ffmpeg_utils.cc.

References append_sep().

Referenced by find_original(), and Cache::load_index().

◆ append_sep()

const std::string & append_sep ( std::string *  path)

Add / to the path if required.

Parameters
[in]path- Path to add separator to.
Returns
Returns constant reference to path.

Definition at line 984 of file ffmpeg_utils.cc.

Referenced by append_filename(), check_bluray(), check_cuesheet(), check_dvd(), check_vcd(), ffmpegfs_getattr(), ffmpegfs_opt_proc(), ffmpegfs_readdir(), VCDUTILS::get_directory(), insert_dir(), main(), parse_cuesheet(), remove_filename(), and transcoder_cache_path().

◆ check_ext()

bool check_ext ( const std::string &  ext,
const std::string &  filename 
)

Check if filename has a certain extension. The check is case sensitive.

Parameters
ext- Extension to check.
filename- Filename to check
Returns
Returns true if extension matches, false if not

Definition at line 1079 of file ffmpeg_utils.cc.

References find_ext().

Referenced by ffmpegfs_getattr().

◆ check_ignore()

bool check_ignore ( size_t  size,
size_t  offset 
)

For use with win_smb_fix=1: Check if this an illegal access offset by Windows.

Parameters
[in]size- sizeof of the file
[in]offset- offset at which file is accessed
Returns
If request should be ignored, returns true; otherwise false

Definition at line 2058 of file ffmpeg_utils.cc.

◆ detect_docker()

bool detect_docker ( )

Detect if we are running under Docker.

Returns
Returns true, if running under Docker, or false if not.

Definition at line 2411 of file ffmpeg_utils.cc.

Referenced by main().

◆ exepath()

void exepath ( std::string *  path)

Path to FFmpegfs binary.

Parameters
[in]path- Path to FFmpegfs binary.

Definition at line 1785 of file ffmpeg_utils.cc.

Referenced by prepare_script().

◆ expand_path()

const std::string & expand_path ( std::string *  tgt,
const std::string &  src 
)

Expand path, e.g., expand ~/ to home directory.

Parameters
[out]tgt- Expanded source path.
[in]src- Path to expand.
Returns
On success, returns expanded source path.

Definition at line 1876 of file ffmpeg_utils.cc.

Referenced by ffmpegfs_opt_proc(), main(), and transcoder_cache_path().

◆ ffmpeg_geterror()

std::string ffmpeg_geterror ( int  errnum)

Get FFmpeg error string for errnum. Internally calls av_strerror().

Parameters
[in]errnum- FFmpeg error code.
Returns
Returns std::string with the error defined by errnum.

Definition at line 1137 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_albumart_frame(), FFmpeg_Transcoder::add_albumart_stream(), FFmpeg_Transcoder::add_external_subtitle_stream(), FFmpeg_Transcoder::add_samples_to_fifo(), FFmpeg_Transcoder::add_stream_copy(), FFmpeg_Transcoder::add_subtitle_stream(), FFmpeg_Transcoder::alloc_picture(), FFmpeg_Transcoder::convert_samples(), FFmpeg_Transcoder::create_audio_frame(), FFmpeg_Transcoder::decode(), FFmpeg_Transcoder::decode_audio_frame(), FFmpeg_Transcoder::decode_frame(), FFmpeg_Transcoder::decode_subtitle(), FFmpeg_Transcoder::decode_video_frame(), FFmpeg_Base::dict_set_with_check(), FFmpeg_Transcoder::encode_audio_frame(), FFmpeg_Transcoder::encode_image_frame(), FFmpeg_Transcoder::encode_subtitle(), FFmpeg_Transcoder::encode_video_frame(), FFmpeg_Transcoder::flush_delayed_audio(), FFmpeg_Transcoder::flush_delayed_video(), FFmpeg_Transcoder::foreach_subtitle_file(), FFmpeg_Transcoder::hwdevice_ctx_add_ref(), FFmpeg_Transcoder::hwdevice_ctx_create(), FFmpeg_Transcoder::hwframe_copy_from_hw(), FFmpeg_Transcoder::hwframe_copy_to_hw(), FFmpeg_Transcoder::hwframe_ctx_set(), FFmpeg_Transcoder::init_audio_output_frame(), FFmpeg_Transcoder::init_converted_samples(), FFmpeg_Transcoder::init_deinterlace_filters(), FFmpeg_Transcoder::init_resampler(), FFmpeg_Transcoder::open_albumarts(), FFmpeg_Transcoder::open_bestmatch_audio(), FFmpeg_Transcoder::open_bestmatch_decoder(), FFmpeg_Transcoder::open_bestmatch_video(), FFmpeg_Transcoder::open_input_file(), FFmpeg_Transcoder::open_subtitles(), FFmpeg_Base::opt_set_with_check(), parse_file(), FFmpeg_Transcoder::read_decode_convert_and_store(), FFmpeg_Transcoder::send_filters(), FFmpeg_Transcoder::skip_decoded_frames(), FFmpeg_Transcoder::start_new_segment(), FFmpeg_Transcoder::store_packet(), transcoder_thread(), FFmpeg_Transcoder::write_output_file_header(), and FFmpeg_Transcoder::write_output_file_trailer().

◆ ffmpeg_libinfo() [1/2]

std::string ffmpeg_libinfo ( )

Get info about the FFmpeg libraries used.

Returns
std::tring with info about the linked FFmpeg libraries.

Definition at line 1235 of file ffmpeg_utils.cc.

◆ ffmpeg_libinfo() [2/2]

static std::string ffmpeg_libinfo ( bool  lib_exists,
__attribute__((unused)) unsigned int  version,
__attribute__((unused)) const char *  cfg,
int  version_minor,
int  version_major,
int  version_micro,
const char *  libname 
)
static

Get FFmpeg library info.

Parameters
[in]lib_exists- Set to true if library exists.
[in]version- Library version number.
[in]cfg- Library configuration.
[in]version_minor- Library version minor.
[in]version_major- Library version major.
[in]version_micro- Library version micro.
[in]libname- Name of the library.
Returns
Formatted library information.

Definition at line 1213 of file ffmpeg_utils.cc.

References strsprintf().

◆ ffmpeg_rescale_q()

int64_t ffmpeg_rescale_q ( int64_t  ts,
const AVRational &  timebase_in,
const AVRational &  timebase_out = av_get_time_base_q() 
)

Convert a FFmpeg time from in timebase to outtime base.

If out time base is omitted, returns standard AV_TIME_BASE fractional seconds Avoids conversion of AV_NOPTS_VALUE.

Parameters
[in]ts- Time in input timebase.
[in]timebase_in- Input timebase.
[in]timebase_out- Output timebase, defaults to AV_TIMEBASE if unset.
Returns
Returns converted value, or AV_NOPTS_VALUE if ts is AV_NOPTS_VALUE.

Definition at line 1151 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_albumart_stream(), FFmpeg_Transcoder::add_stream(), FFmpeg_Transcoder::add_stream_copy(), FFmpeg_Transcoder::add_subtitle_stream(), FFmpeg_Transcoder::create_audio_frame(), FFmpeg_Transcoder::decode(), FFmpeg_Transcoder::decode_frame(), FFmpeg_Transcoder::encode_subtitle(), FFmpeg_Transcoder::encode_video_frame(), FFmpeg_Transcoder::open_bestmatch_audio(), FFmpeg_Transcoder::open_bestmatch_video(), FFmpeg_Transcoder::open_input_file(), FFmpeg_Transcoder::read_decode_convert_and_store(), and FFmpeg_Transcoder::start_new_segment().

◆ ffmpeg_rescale_q_rnd()

int64_t ffmpeg_rescale_q_rnd ( int64_t  ts,
const AVRational &  timebase_in,
const AVRational &  timebase_out = av_get_time_base_q() 
)

Convert a FFmpeg time from in timebase to out timebase with rounding.

If out time base is omitted, returns standard AV_TIME_BASE fractional seconds Avoids conversion of AV_NOPTS_VALUE.

Parameters
[in]ts- Time in input timebase.
[in]timebase_in- Input timebase.
[in]timebase_out- Output timebase, defaults to AV_TIMEBASE if unset.
Returns
Returns converted value, or AV_NOPTS_VALUE if ts is AV_NOPTS_VALUE.

Definition at line 1166 of file ffmpeg_utils.cc.

References AV_ROUND_PASS_MINMAX.

Referenced by FFmpeg_Base::audio_info(), FFmpeg_Transcoder::create_audio_frame(), FFmpeg_Transcoder::decode_frame(), FFmpeg_Transcoder::decode_video_frame(), FFmpeg_Transcoder::open_output(), FFmpeg_Transcoder::store_packet(), and FFmpeg_Base::video_info().

◆ file_exists()

bool file_exists ( const std::string &  filename)

Check if file exists.

Parameters
[in]filename- File to check.
Returns
Returns true if file exists, false if not.

Definition at line 2092 of file ffmpeg_utils.cc.

Referenced by Buffer::segment_exists().

◆ find_ext()

bool find_ext ( std::string *  ext,
const std::string &  filename 
)

Find extension in filename, if existing.

Parameters
[in]ext- Extension, if found.
[in]filename- Filename to inspect.
Returns
Returns true if extension was found, false if there was none

Definition at line 1059 of file ffmpeg_utils.cc.

Referenced by check_ext(), ffmpegfs_readdir(), find_original(), and virtual_name().

◆ format_bitrate()

std::string format_bitrate ( BITRATE  value)

Format a bit rate.

Parameters
[in]value- Bit rate to format.
Returns
Returns std::string with formatted value in bit/s, kbit/s or Mbit/s. If value == AV_NOPTS_VALUE returns "unset".

Definition at line 1506 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_stream(), FFmpeg_Base::audio_info(), create_bluray_virtualfile(), create_dvd_virtualfile(), and FFmpeg_Base::video_info().

◆ format_duration()

std::string format_duration ( int64_t  value,
uint32_t  fracs = 3 
)

Format a time in format HH:MM:SS.fract.

Parameters
[in]value- Time value in AV_TIME_BASE factional seconds.
[in]fracs- Fractional digits.
Returns
Returns std::string with formatted value. If value == AV_NOPTS_VALUE returns "unset".

Definition at line 1552 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Base::audio_info(), create_bluray_virtualfile(), create_cuesheet_virtualfile(), create_dvd_virtualfile(), create_vcd_virtualfile(), FFmpeg_Transcoder::decode_frame(), FFmpeg_Transcoder::decode_video_frame(), FFmpeg_Transcoder::store_packet(), and FFmpeg_Base::video_info().

◆ format_number()

std::string format_number ( int64_t  value)

Format numeric value.

Parameters
[in]value- Value to format.
Returns
Returns std::string with formatted value; if value == AV_NOPTS_VALUE returns "unset"; "unlimited" if value == 0.

Definition at line 1490 of file ffmpeg_utils.cc.

◆ format_result_size()

std::string format_result_size ( size_t  size_resulting,
size_t  size_predicted 
)

Format size of transcoded file including difference between predicted and resulting size.

Parameters
[in]size_resulting- Resulting size.
[in]size_predicted- Predicted size.
Returns
Returns std::string with formatted value in bytes plus difference; if value == AV_NOPTS_VALUE returns "unset"; "unlimited" if value == 0.

Definition at line 1677 of file ffmpeg_utils.cc.

References format_size().

◆ format_result_size_ex()

std::string format_result_size_ex ( size_t  size_resulting,
size_t  size_predicted 
)

Format size of transcoded file including difference between predicted and resulting size.

Parameters
[in]size_resulting- Resulting size.
[in]size_predicted- Predicted size.
Returns
Returns std::string with formatted value in bytes plus KB, MB or TB and difference; if value == AV_NOPTS_VALUE returns "unset"; "unlimited" if value == 0.

Definition at line 1691 of file ffmpeg_utils.cc.

References format_size(), and strsprintf().

Referenced by transcode_finish().

◆ format_samplerate()

std::string format_samplerate ( int  value)

Format a samplerate.

Parameters
[in]value- Sample rate to format.
Returns
Returns std::string with formatted value in Hz or kHz. If value == AV_NOPTS_VALUE returns "unset".

Definition at line 1530 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_stream(), FFmpeg_Base::audio_info(), create_bluray_virtualfile(), and FFmpeg_Transcoder::init_resampler().

◆ format_size()

std::string format_size ( uint64_t  value)

Format size.

Parameters
[in]value- Size to format.
Returns
Returns std::string with formatted value in bytes, KB, MB or TB; if value == AV_NOPTS_VALUE returns "unset"; "unlimited" if value == 0.

Definition at line 1632 of file ffmpeg_utils.cc.

Referenced by create_bluray_virtualfile(), create_dvd_virtualfile(), format_result_size(), format_result_size_ex(), Buffer::map_file(), and transcode().

◆ format_size_ex()

std::string format_size_ex ( uint64_t  value)

Format size.

Parameters
[in]value- Size to format.
Returns
Returns std::string with formatted value in bytes plus KB, MB or TB; if value == AV_NOPTS_VALUE returns "unset"; "unlimited" if value == 0.

Definition at line 1671 of file ffmpeg_utils.cc.

Referenced by transcode_finish(), transcoder_predict_filesize(), and transcoder_set_filesize().

◆ format_time()

std::string format_time ( time_t  value)

Format a time in format "w d m s".

Parameters
[in]value- Time value in AV_TIME_BASE factional seconds.
Returns
Returns std::string with formatted value. If value == AV_NOPTS_VALUE returns "unset".

Definition at line 1579 of file ffmpeg_utils.cc.

Referenced by start_timer(), and transcode().

◆ fourcc_make_string()

std::string fourcc_make_string ( std::string *  buf,
uint32_t  fourcc 
)

Fill the provided buffer with a string containing a FourCC (four-character code) representation.

Parameters
[in]buf- Upon return, filled in with the FourCC representation.
[in]fourcc- The fourcc to represent
Returns
The buffer in input.

Definition at line 1776 of file ffmpeg_utils.cc.

◆ get_audio_props()

int get_audio_props ( AVFormatContext *  format_ctx,
int *  channels,
int *  samplerate 
)

Get first audio stream.

Parameters
[in]format_ctx- Format context of file
[out]channels- Number of audio channels in stream
[out]samplerate- Audio sample rate of stream
Returns
Returns stream number (value greater or equal zero) or negative errno value

Definition at line 2460 of file ffmpeg_utils.cc.

Referenced by parse_file().

◆ get_codec_name()

const char * get_codec_name ( AVCodecID  codec_id,
bool  long_name 
)

Safe way to get the codec name. Function never fails, will return "unknown" on error.

Parameters
[in]codec_id- ID of codec
[in]long_name- If true, gets the long name.
Returns
Returns the codec name or "unknown" on error.

Definition at line 1353 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_albumart_stream(), FFmpeg_Transcoder::add_subtitle_stream(), FFmpeg_Base::audio_info(), FFmpeg_Transcoder::calculate_predicted_filesize(), FFmpeg_Transcoder::open_bestmatch_video(), FFmpeg_Base::subtitle_info(), transcoder_set_filesize(), and FFmpeg_Base::video_info().

◆ get_disk_free()

size_t get_disk_free ( std::string &  path)

Get free disk space.

Parameters
[in]path- Path or file on disk.
Returns
Returns the free disk space.

Definition at line 2046 of file ffmpeg_utils.cc.

◆ get_encoding()

int get_encoding ( const char *  str,
std::string &  encoding 
)

Try to detect the encoding of str. This is relatively realiable, but may be wrong.

Parameters
[in]str- Text string to be checked.
[out]encoding- Detected encoding.
Returns
Returns 0 if successful, or CHARDET_OUT_OF_MEMORY/CHARDET_MEM_ALLOCATED_FAIL on error.

Definition at line 2161 of file ffmpeg_utils.cc.

◆ get_filetype()

FILETYPE get_filetype ( const std::string &  desttype)

Get the FFmpegfs filetype, desttype must be one of FFmpeg's "official" short names for formats.

Parameters
[in]desttype- Destination type (MP4, WEBM etc.).
Returns
On success, returns FILETYPE enum; On error, returns FILETYPE_UNKNOWN.

Definition at line 1443 of file ffmpeg_utils.cc.

References filetype_map.

Referenced by FFmpegfs_Format::init().

◆ get_filetype_from_list()

FILETYPE get_filetype_from_list ( const std::string &  desttypelist)

Get the FFmpegfs filetype, desttypelist must be a comma separated list of FFmpeg's "official" short names for formats. Will return the first match. Same as get_filetype, but accepts a comma separated list.

Parameters
[in]desttypelist- Destination type list (MP4, WEBM etc.) separated by commas.
Returns
On success, returns FILETYPE enum; On error, returns FILETYPE_UNKNOWN.

Definition at line 1467 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::open_input_file().

◆ get_filetype_text()

std::string get_filetype_text ( FILETYPE  filetype)

Convert FILETYPE enum to human readable text.

Parameters
[in]filetype- FILETYPE enum value to convert.
Returns
FILETYPE enum as text or "INVALID" if not known.

Definition at line 1456 of file ffmpeg_utils.cc.

Referenced by transcoder_set_filesize().

◆ get_media_type_string()

const char * get_media_type_string ( enum AVMediaType  media_type)

av_get_media_type_string is missing, so we provide our own.

Parameters
[in]media_type- Media type to map.
Returns
Pointer to media type string.

Definition at line 1182 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_albumart_stream(), FFmpeg_Transcoder::add_subtitle_stream(), and FFmpeg_Transcoder::open_bestmatch_decoder().

◆ hwdevice_get_type_name()

const char * hwdevice_get_type_name ( AVHWDeviceType  dev_type)

Save version of hwdevice_get_type_name: Get the string name of an AVHWDeviceType.

Parameters
[in]dev_type- Type from enum AVHWDeviceType.
Returns
Pointer to a static string containing the name, or "unknown" if the type is not valid.

Definition at line 2111 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::get_hw_pix_fmt(), and FFmpeg_Transcoder::hwdevice_ctx_create().

◆ init_id3v1()

void init_id3v1 ( ID3v1 id3v1)

Initialise ID3v1 tag.

Definition at line 1480 of file ffmpeg_utils.cc.

Referenced by Cache_Entry::clear(), and FFmpeg_Transcoder::FFmpeg_Transcoder().

◆ is_album_art()

bool is_album_art ( AVCodecID  codec_id,
const AVRational *  frame_rate = nullptr 
)

Minimal check if codec is an album art. Requires frame_rate to decide whether this is a video stream if codec_id is not BMP or PNG (which means its undoubtedly an album art). For MJPEG this may also be a video stream if the frame rate is high enough.

Parameters
[in]codec_id- ID of codec.
[in]frame_rate- Video frame rate, if known.
Returns
Returns true if codec is for an image; false if not.

Definition at line 2012 of file ffmpeg_utils.cc.

Referenced by guess_format_idx(), FFmpeg_Transcoder::is_video(), FFmpeg_Transcoder::open_albumarts(), and parse_file().

◆ is_blocked()

bool is_blocked ( const std::string &  filename)

Check if filename should be hidden from output path.

Parameters
[in]filename- Name to check
Returns
Returns true, if filename is blocked, false if not.

Definition at line 2507 of file ffmpeg_utils.cc.

Referenced by ffmpegfs_getattr(), and ffmpegfs_readdir().

◆ is_device()

static int is_device ( __attribute__((unused)) const AVClass *  avclass)
static

Check if class is a FMmpeg device.

Todo:
Currently always returns 0. Must implement real check.
Parameters
[in]avclass- Private class object
Returns
Returns 1 if object is a device, 0 if not.

Definition at line 1260 of file ffmpeg_utils.cc.

◆ is_mount()

int is_mount ( const std::string &  path)

Check if path is a mount.

Parameters
[in]path- Path to check.
Returns
Returns 1 if path is a mount point; 0 if not. On error, returns -1. Check errorno for details.

Definition at line 1892 of file ffmpeg_utils.cc.

References new_strdup().

Referenced by ffmpegfs_opt_proc().

◆ is_selected()

bool is_selected ( const std::string &  ext)

Find extension in include list, if existing.

Parameters
[in]ext- Extension, if found.
Returns
Returns true if extension was found, false if there was none

Definition at line 2494 of file ffmpeg_utils.cc.

Referenced by virtual_name().

◆ is_text_codec()

bool is_text_codec ( AVCodecID  codec_id)

Check if subtitle codec is a text or graphical codec.

Parameters
[in]codec_id- Codec to check, must be one of the subtitle codecs.
Returns
Returns true if codec_id is a text based codec, false if it is bitmap based.

Definition at line 2428 of file ffmpeg_utils.cc.

Referenced by Format_Options::subtitle_codec().

◆ ltrim()

std::string & ltrim ( std::string &  s)

trim from start

Parameters
[in]s- String to trim.
Returns
Reference to string s.

Definition at line 1800 of file ffmpeg_utils.cc.

◆ make_filename()

std::string make_filename ( uint32_t  file_no,
const std::string &  fileext 
)

Make a file name from file number and file extension.

Parameters
[in]file_no- File number 1...n
[in]fileext- Extension of file (e.g mp4, webm)
Returns
Returns the file name.

Definition at line 2086 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::encode_finish(), ffmpegfs_getattr(), ffmpegfs_readdir(), Buffer::init(), and make_hls_fileset().

◆ make_lower()

void make_lower ( std::string *  input)

Convert string to lower case.

Parameters
[in,out]inputString to convert

Definition at line 2104 of file ffmpeg_utils.cc.

◆ make_upper()

void make_upper ( std::string *  input)

Convert string to upper case.

Parameters
[in,out]inputString to convert

Definition at line 2097 of file ffmpeg_utils.cc.

◆ mktree()

int mktree ( const std::string &  path,
mode_t  mode 
)

Make directory tree.

Parameters
[in]path- Path to create
[in]mode- Directory mode, see mkdir() function.
Returns
On success, returns 0; on error, returns non-zero errno value.

Definition at line 1376 of file ffmpeg_utils.cc.

Referenced by Buffer::init(), and Cache::load_index().

◆ mssleep()

void mssleep ( int  milliseconds)

Sleep for specified time.

Parameters
milliseconds- Milliseconds to sleep

Definition at line 2539 of file ffmpeg_utils.cc.

Referenced by transcode(), transcode_until(), and transcoder_read_frame().

◆ new_strdup()

char * new_strdup ( const std::string &  str)

strdup() variant taking a std::string as input.

Parameters
[in]str- String to duplicate.
Returns
Copy of the input string. Remember to delete the allocated memory.

Definition at line 1122 of file ffmpeg_utils.cc.

Referenced by Buffer::init(), is_mount(), remove_filename(), and remove_path().

◆ nocasecompare()

int nocasecompare ( const std::string &  lhs,
const std::string &  rhs 
)

nocasecompare to make std::string find operations case insensitive

Parameters
[in]lhs- left hand string
[in]rhs- right hand string
Returns
-1 if lhs < rhs; 0 if lhs == rhs and 1 if lhs > rhs

Definition at line 2041 of file ffmpeg_utils.cc.

Referenced by comp::operator()().

◆ nssleep()

void nssleep ( int  nanoseconds)

Sleep for specified time.

Parameters
nanoseconds- Nanoseconds to sleep

Definition at line 2549 of file ffmpeg_utils.cc.

◆ print_fps()

static void print_fps ( double  d,
const char *  postfix 
)
static

Print frames per second.

Parameters
[in]d- Frames per second.
[in]postfix- Postfix text.

Definition at line 1712 of file ffmpeg_utils.cc.

◆ print_stream_info()

int print_stream_info ( const AVStream *  stream)

Print info about an AVStream.

Parameters
[in]stream- Stream to print.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 1733 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_stream_copy().

◆ read_file()

int read_file ( const std::string &  path,
std::string &  result 
)

Read text file and return in UTF-8 format, no matter in which encoding the input file is. UTF-8/16/32 with BOM will always return a correct result. For all other encodings the function tries to detect it, that may fail.

Parameters
[in]path- Path and filename of input file
[out]result- File contents as UTF-8
Returns
Returns one of the ENCODING enum values on success, or errno on error. Basically a return code > 0 means there is an error.

Definition at line 2203 of file ffmpeg_utils.cc.

Referenced by parse_cuesheet_file().

◆ reg_compare()

int reg_compare ( const std::string &  value,
const std::string &  pattern,
std::regex::flag_type  flag = std::regex::ECMAScript 
)

Compare value with pattern.

Parameters
[in]value- Value to check.
[in]pattern- Regexp pattern to match.
[in]flag- On of the flag_type constants, see https://en.cppreference.com/w/cpp/regex/basic_regex for options. Mostly std::regex::icase is used to make matches case insensitive.
Returns
Returns 0 if pattern matches; 1 if not; -1 if pattern is no valid regex

Definition at line 1854 of file ffmpeg_utils.cc.

Referenced by get_bitrate(), get_samplerate(), get_size(), and get_time().

◆ regex_escape()

const std::string & regex_escape ( std::string *  str)

Escape characters that are meaningful to regexp.

Parameters
[in]str- String to escape
Returns
Returns reference to string.

Definition at line 2478 of file ffmpeg_utils.cc.

Referenced by FFmpeg_Transcoder::add_external_subtitle_streams().

◆ remove_ext()

const std::string & remove_ext ( std::string *  filepath)

Remove extension from filename.

Parameters
[in]filepath- Filename to remove path from.
Returns
Returns constant reference to filename.

Definition at line 1044 of file ffmpeg_utils.cc.

Referenced by ffmpegfs_getattr(), and find_original().

◆ remove_filename()

const std::string & remove_filename ( std::string *  filepath)

Remove filename from path. Handy dirname alternative.

Parameters
[in]filepath- Path to remove filename from.
Returns
Returns constant reference to path.

Definition at line 1013 of file ffmpeg_utils.cc.

References append_sep(), and new_strdup().

Referenced by ffmpegfs_getattr(), find_original(), find_parent(), VCDUTILS::get_directory(), load_path(), parse_cuesheet(), and FileIO::set_virtualfile().

◆ remove_path()

const std::string & remove_path ( std::string *  filepath)

Remove path from filename. Handy basename alternative.

Parameters
[in]filepath- Filename to remove path from.
Returns
Returns constant reference to filename.

Definition at line 1029 of file ffmpeg_utils.cc.

References new_strdup().

Referenced by check_cuesheet(), find_original(), get_number(), load_path(), and parse_cuesheet().

◆ remove_sep()

const std::string & remove_sep ( std::string *  path)

Remove / from the path.

Parameters
[in]path- Path to remove separator from.
Returns
Returns constant reference to path.

Definition at line 1003 of file ffmpeg_utils.cc.

Referenced by find_parent(), load_path(), and make_hls_fileset().

◆ replace_all() [1/2]

std::string replace_all ( std::string *  str,
const std::string &  from,
const std::string &  to = "" 
)

Same as std::string replace(), but replaces string in-place.

Parameters
[in,out]str- Source string.
[in]from- String to replace.
[in]to- Replacement string.
Returns
Source string with all occurrences of from replaced with to.

Definition at line 1822 of file ffmpeg_utils.cc.

◆ replace_all() [2/2]

std::string replace_all ( std::string  str,
const std::string &  from,
const std::string &  to 
)

Same as std::string replace(), but replaces all occurrences.

Parameters
[in,out]str- Source string.
[in]from- String to replace.
[in]to- Replacement string.
Returns
Source string with all occurrences of from replaced with to.

Definition at line 1817 of file ffmpeg_utils.cc.

Referenced by create_bluray_virtualfile(), create_cuesheet_virtualfile(), create_dvd_virtualfile(), create_vcd_virtualfile(), and parse_file().

◆ replace_ext()

const std::string & replace_ext ( std::string *  filepath,
const std::string &  ext 
)

Replace extension in filename, taking into account that there might not be an extension already.

Parameters
[in]filepath- Filename to replace extension.
[in]ext- Extension to replace.
Returns
Returns constant reference to filename.

Definition at line 1085 of file ffmpeg_utils.cc.

Referenced by check_cuesheet(), and virtual_name().

◆ replace_start()

bool replace_start ( std::string *  str,
const std::string &  from,
const std::string &  to = "" 
)

Replace start of string from "from" to "to".

Parameters
[in,out]str- Source string.
[in]from- String to replace.
[in]to- Replacement string.
Returns
True if from has been replaced, false if not.

Definition at line 1833 of file ffmpeg_utils.cc.

Referenced by insert_file(), and Logging::Logger::~Logger().

◆ rtrim()

std::string & rtrim ( std::string &  s)

trim from end

Parameters
[in]s- String to trim.
Returns
Reference to string s.

Definition at line 1806 of file ffmpeg_utils.cc.

◆ sanitise_filepath() [1/2]

std::string sanitise_filepath ( const std::string &  filepath)

Sanitise file name. Calls realpath() to remove duplicate // or resolve ../.. etc.

Parameters
[in]filepath- File name and path to sanitise.
Returns
Returns sanitised file name and path.

Definition at line 1994 of file ffmpeg_utils.cc.

◆ sanitise_filepath() [2/2]

std::string sanitise_filepath ( std::string *  filepath)

Sanitise file name. Calls realpath() to remove duplicate // or resolve ../.. etc. Changes the path in place.

Parameters
[in]filepath- File name and path to sanitise.
Returns
Returns sanitised file name and path.

Definition at line 1977 of file ffmpeg_utils.cc.

Referenced by ffmpegfs_opt_proc(), find_file(), find_file_from_orig(), find_original(), and insert_file().

◆ save_free()

void save_free ( void **  p)

Savely free memory: Pointer will be set to nullptr before it is actually freed.

Parameters
[in,out]p- Pointer to delete

Definition at line 2530 of file ffmpeg_utils.cc.

◆ show_caps()

int show_caps ( int  device_only)

Lists all supported codecs and devices.

Parameters
[in]device_only- If true lists devices only.
Returns
On success, returns 0; on error, a negative AVERROR value.

Definition at line 1269 of file ffmpeg_utils.cc.

◆ split()

std::vector< std::string > split ( const std::string &  input,
const std::string &  regex 
)

Split string into an array delimited by a regular expression.

Parameters
[in]input- Input string.
[in]regex- Regular expression to match.
Returns
Returns an array with separate elements.

Definition at line 1968 of file ffmpeg_utils.cc.

Referenced by get_desttype(), and get_value().

◆ stat_set_size()

void stat_set_size ( struct stat *  st,
size_t  size 
)

Properly fill in all size related members in stat struct.

Parameters
[in,out]ststat structure to update
[in]sizesize value to copy

Definition at line 2401 of file ffmpeg_utils.cc.

Referenced by create_cuesheet_virtualfile(), FFmpeg_Transcoder::encode_finish(), ffmpegfs_fgetattr(), ffmpegfs_getattr(), init_stat(), load_path(), transcoder_cached_filesize(), and transcoder_read_frame().

◆ strcasecmp()

int strcasecmp ( const std::string &  s1,
const std::string &  s2 
)

strcasecmp() equivalent for std::string.

Parameters
[in]s1- std:string #1
[in]s2- std:string #2
Returns
Returns same as strcasecmp() for char *.

Definition at line 1849 of file ffmpeg_utils.cc.

Referenced by append_ext(), FFmpeg_Transcoder::copy_metadata(), and find_original().

◆ supports_albumart()

int supports_albumart ( FILETYPE  filetype)

Check if file type supports album arts.

Parameters
[in]filetype- File type: MP3, MP4 etc.
Returns
Returns true if album arts are supported, false if not.

Definition at line 1437 of file ffmpeg_utils.cc.

◆ tempdir()

void tempdir ( std::string &  path)

Get temporary directory.

Parameters
[out]path- Path to temporary directory.

Definition at line 1417 of file ffmpeg_utils.cc.

◆ to_utf8()

int to_utf8 ( std::string &  text,
const std::string &  encoding 
)

Convert almost any encoding to UTF-8. To get a list of all possible encodings run "iconv --list".

Parameters
[in]text- Text to be converted
[in]encoding- Encoding of input text.
Returns
Returns 0 if successful and the converted text, or errno value On error, and text is unchanged.

Definition at line 2117 of file ffmpeg_utils.cc.

◆ trim()

std::string & trim ( std::string &  s)

trim from both ends

Parameters
[in]s- String to trim.
Returns
Reference to string s.

Definition at line 1812 of file ffmpeg_utils.cc.

Referenced by VCDUTILS::convert_txt2string(), and Logging::Logger::~Logger().

◆ ussleep()

void ussleep ( int  microseconds)

Sleep for specified time.

Parameters
microseconds- Microseconds to sleep

Definition at line 2544 of file ffmpeg_utils.cc.

Variable Documentation

◆ filetype_map

const FILETYPE_MAP filetype_map
static
Initial value:
=
{
{ "mp3", FILETYPE_MP3 },
{ "mp4", FILETYPE_MP4 },
{ "wav", FILETYPE_WAV },
{ "ogg", FILETYPE_OGG },
{ "webm", FILETYPE_WEBM },
{ "mov", FILETYPE_MOV },
{ "aiff", FILETYPE_AIFF },
{ "opus", FILETYPE_OPUS },
{ "prores", FILETYPE_PRORES },
{ "alac", FILETYPE_ALAC },
{ "png", FILETYPE_PNG },
{ "jpg", FILETYPE_JPG },
{ "bmp", FILETYPE_BMP },
{ "ts", FILETYPE_TS },
{ "hls", FILETYPE_HLS },
{ "flac", FILETYPE_FLAC },
{ "mkv", FILETYPE_MKV },
}

List of supported file types

Definition at line 104 of file ffmpeg_utils.cc.

Referenced by get_filetype().