Skip to content

Effort free video editing!

Never use ffmpeg protocol, treat name as literal

WyattBluec79ac346baf5
diff --git a/src/av.nim b/src/av.nim
index 784f7c7f..c9dec41a 100644
--- a/src/av.nim
+++ b/src/av.nim
@@ -1,4 +1,5 @@
import std/[math, sequtils, strformat, strutils, tables]
+from std/os import getFileInfo
import ./[ffmpeg, log]
import ./lib/[audioutil, lang, suggest]
import ./util/rational
@@ -306,8 +307,23 @@ type InputContainer* = object
audio*: seq[ptr AVStream]
subtitle*: seq[ptr AVStream]
+func fileUrl*(path: string): string =
+ ## `path` for FFmpeg to open as the file it names. Given a bare path, FFmpeg
+ ## takes what comes before a colon for a protocol, so `a:b.mp4` isn't found
+ ## and `file:b.mp4` is `b.mp4`.
+ "file:" & path
+
proc openFormatCtx*(file: cstring): ptr AVFormatContext {.raises: [IOError].} =
- let ret = avformat_open_input(addr result, file, nil, nil)
+ # For a name with `%d` in it FFmpeg picks the image demuxer without opening
+ # the file, so one that isn't there would come back as an empty input.
+ try: discard getFileInfo($file)
+ except OSError: raise newException(IOError, &"Input file doesn't exist: {file}")
+ # That demuxer otherwise reads `%d` as a frame number and opens
+ # `shot0.png`, `shot1.png`, ... for `shot%d.png`.
+ var options: ptr AVDictionary = nil
+ discard av_dict_set(addr options, "pattern_type", "none", 0)
+ let ret = avformat_open_input(addr result, fileUrl($file).cstring, nil, addr options)
+ av_dict_free(addr options)
if ret == AVERROR(ENOENT):
raise newException(IOError, &"Input file doesn't exist: {file}")
elif ret == AVERROR_INVALIDDATA:
@@ -620,7 +636,10 @@ type OutputContainer* = object
proc openWrite*(file: string, format: ptr AVOutputFormat = nil): OutputContainer =
## Without a `format`, FFmpeg guesses one from the extension of `file`.
let formatCtx: ptr AVFormatContext = nil
- discard avformat_alloc_output_context2(addr formatCtx, format, nil, file.cstring)
+ # As a URL here too: a muxer can open its own output again by this name,
+ # as mov does to move the index to the front.
+ discard avformat_alloc_output_context2(addr formatCtx, format, nil,
+ fileUrl(file).cstring)
if formatCtx == nil:
error "Could not create output context"
@@ -828,7 +847,7 @@ proc startEncoding*(self: var OutputContainer) =
# Open the output file, if needed.
if (outputCtx.oformat.flags and AVFMT_NOFILE) == 0:
- if avio_open(addr outputCtx.pb, self.file.cstring, AVIO_FLAG_WRITE) < 0:
+ if avio_open(addr outputCtx.pb, fileUrl(self.file).cstring, AVIO_FLAG_WRITE) < 0:
error &"Could not open output file '{self.file}'"
if self.streaming:
let pb = cast[ptr AVIOContext](outputCtx.pb)
diff --git a/src/wavutil.nim b/src/wavutil.nim
index f3d9ed99..448c9fd6 100644
--- a/src/wavutil.nim
+++ b/src/wavutil.nim
@@ -208,7 +208,7 @@ proc transcodeAudio*(inputPath, outputPath: string, streamIndex: int32,
let outputCtx: ptr AVFormatContext = nil
ret = avformat_alloc_output_context2(addr outputCtx, nil, nil,
- outputPath.cstring)
+ fileUrl(outputPath).cstring)
if outputCtx == nil:
error "Could not create output context"
@@ -265,7 +265,7 @@ proc transcodeAudio*(inputPath, outputPath: string, streamIndex: int32,
outputStream.codecpar.codec_tag = 0
if (outputCtx.oformat.flags and AVFMT_NOFILE) == 0:
- if avio_open(addr outputCtx.pb, outputPath.cstring, AVIO_FLAG_WRITE) < 0:
+ if avio_open(addr outputCtx.pb, fileUrl(outputPath).cstring, AVIO_FLAG_WRITE) < 0:
error &"Could not open output file '{outputPath}'"
if avformat_write_header(outputCtx, nil) < 0: