-1

I have a video file in the project folder that is detected by the app as VideoPlayer(player: AVPlayer(url: url)) does make use of it and play it on the iPhone screen, I have the video path of simply videoURL = Bundle.main.url(forResource: "Earth_Zoom_In", withExtension: "mov")

Now, a kind gentleman gave me this code just yesterday:

func syncCommand() {
    guard let session = FFmpegKit.execute("-i Earth_Zoom_In.mov -c:v file1.mp4") else {
        print("!! Failed to create session")
        return
    }
    let returnCode = session.getReturnCode()
    if ReturnCode.isSuccess(returnCode) {
        print("COOOOL");
    } else if ReturnCode.isCancel(returnCode) {
        print("CANCELED???? WHAT?????");
    } else {
        print("Command failed with state \(FFmpegKitConfig.sessionState(toString: session.getState()) ?? "Unknown") and rc \(returnCode?.description ?? "Unknown").\(session.getFailStackTrace() ?? "Unknown")")
    }
}

But Ffmpeg seems to not find the file as when the function is executed I get ERROR: Earth_Zoom_In.mov: No such file or directory

But it is on the directory...

Can anyone help me with that? please.

I have tried:

guard let session = FFmpegKit.execute("-i \(url) -c:v file1.mp4") else {

inside of syncCommand(url: URL?) and change the code in my init

to init() {
        videoURL = Bundle.main.url(forResource: "Earth_Zoom_In", withExtension: "mov")
        syncCommand(url: videoURL);
    }

so video URL is on scope... but again, the same error, just with a complete path this time...

WARNING: Trailing option(s) found in the command: may be ignored.
2022-06-19 22:25:08.541915-0500 videommm[7736:217682] ERROR: Optional(file:///Users/homefolder/Library/Developer/CoreSimulator/Devices/2FE26EEA-A16A-4DAA-A38E-D003C678A3A2/data/Containers/Bundle/Application/374A618D-37CB-4AC2-92E6-BD3D22185EA4/videommm.app/Earth_Zoom_In.mov): No such file or directory

In case you were wondering about my file structure:

enter image description here

ENTIRE ERROR LOG AFTER I TRIED

guard let input = Bundle.main.url(forResource: "Earth_Zoom_In", withExtension: "mov") else { return }
    guard let outputPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else { return }
    let output = outputPath.appendingPathComponent("file1.mp4")
    guard let session = FFmpegKit.execute("-i \(input.path) -c:v \(outputPath.path)")

(above solution by @MadProgrammer )::::

2022-06-19 22:59:42.004358-0500 videommm[8918:255076] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x60000140c340> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-06-19 22:59:42.048383-0500 videommm[8918:254980] <CATransformLayer: 0x6000014060e0> - changing property masksToBounds in transform-only layer, will have no effect
2022-06-19 22:59:42.049907-0500 videommm[8918:254980] <CATransformLayer: 0x60000143bc20> - changing property masksToBounds in transform-only layer, will have no effect
2022-06-19 22:59:42.085850-0500 videommm[8918:254980] <CATransformLayer: 0x600001408d00> - changing property masksToBounds in transform-only layer, will have no effect
2022-06-19 22:59:42.108774-0500 videommm[8918:254980] <CATransformLayer: 0x600001406e20> - changing property masksToBounds in transform-only layer, will have no effect
2022-06-19 22:59:42.110957-0500 videommm[8918:254980] <CATransformLayer: 0x60000143e0e0> - changing property masksToBounds in transform-only layer, will have no effect
2022-06-19 22:59:42.154667-0500 videommm[8918:254980] <CATransformLayer: 0x600001408d00> - changing property allowsGroupBlending in transform-only layer, will have no effect
2022-06-19 22:59:42.155013-0500 videommm[8918:254980] <CATransformLayer: 0x600001406e20> - changing property allowsGroupBlending in transform-only layer, will have no effect
2022-06-19 22:59:46.660559-0500 videommm[8918:254980] Loading ffmpeg-kit.
2022-06-19 22:59:46.663426-0500 videommm[8918:254980] Loaded ffmpeg-kit-full-x86_64-4.5.1-20220114
2022-06-19 22:59:46.664216-0500 videommm[8918:255076] INFO: ffmpeg version v4.5-dev-3393-g30322ebe3c
2022-06-19 22:59:46.664496-0500 videommm[8918:255076] INFO:  Copyright (c) 2000-2021 the FFmpeg developers
2022-06-19 22:59:46.664704-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.664909-0500 videommm[8918:255076] INFO:   built with Apple clang version 13.0.0 (clang-1300.0.29.30)
2022-06-19 22:59:46.665124-0500 videommm[8918:255076] INFO:   configuration: --cross-prefix=x86_64-ios-darwin- --sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator15.2.sdk --prefix=/Users/taner/Projects/ffmpeg-kit/prebuilt/apple-ios-x86_64/ffmpeg --pkg-config=/opt/homebrew/bin/pkg-config --enable-version3 --arch=x86_64 --cpu=x86_64 --target-os=darwin --disable-neon --disable-asm --ar=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ar --cc=clang --cxx=clang++ --as='clang -arch x86_64 -target x86_64-apple-ios12.1-simulator -march=x86-64 -msse4.2 -mpopcnt -m64 -DFFMPEG_KIT_X86_64 -Wno-unused-function -Wno-deprecated-declarations -fstrict-aliasing -DIOS -DFFMPEG_KIT_BUILD_DATE=20220114 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator15.2.sdk -O2 -mios-simulator-version-min=12.1 -I/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator15.2.sdk/usr/include' --ranlib=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib --strip=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip --nm=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/nm --extra-ldflags='-mios-simulator-version-min=12.1' --disable-autodetect --enable-cross-compile --enable-pic --enable-inline-asm --enable-optimizations --enable-swscale --enable-shared --disable-static --install-name-dir='@rpath' --enable-pthreads --disable-v4l2-m2m --disable-outdev=v4l2 --disable-outdev=fbdev --disable-indev=v4l2 --disable-indev=fbdev --enable-small --disable-xmm-clobber-test --disable-debug --disable-neon-clobber-test --disable-programs --disable-postproc --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-sndio --disable-schannel --disable-securetransport --disable-xlib --disable-cuda --disable-cuvid --disable-nvenc --disable-vaapi --disable-vdpau --disable-alsa --disable-cuda --disable-cuvid --disable-nvenc --disable-vaapi --disable-vdpau --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gmp --enable-gnutls --enable-libmp3lame --enable-libass --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libxml2 --enable-libopencore-amrnb --enable-libshine --enable-libspeex --enable-libdav1d --enable-libkvazaar --enable-libilbc --enable-libopus --enable-libsnappy --enable-libsoxr --enable-libtwolame --disable-sdl2 --enable-libvo-amrwbenc --enable-libzimg --disable-openssl --enable-zlib --enable-audiotoolbox --disable-outdev=audiotoolbox --enable-bzlib --enable-videotoolbox --enable-avfoundation --enable-iconv --disable-coreimage --disable-appkit --disable-opencl --disable-opengl
2022-06-19 22:59:46.666175-0500 videommm[8918:255076] INFO:   libavutil      57. 13.100 / 57. 13.100
2022-06-19 22:59:46.666715-0500 videommm[8918:255076] INFO:   libavcodec     59. 15.102 / 59. 15.102
2022-06-19 22:59:46.666956-0500 videommm[8918:255076] INFO:   libavformat    59. 10.100 / 59. 10.100
2022-06-19 22:59:46.667271-0500 videommm[8918:255076] INFO:   libavdevice    59.  1.100 / 59.  1.100
2022-06-19 22:59:46.667512-0500 videommm[8918:255076] INFO:   libavfilter     8. 21.100 /  8. 21.100
2022-06-19 22:59:46.667769-0500 videommm[8918:255076] INFO:   libswscale      6.  1.102 /  6.  1.102
2022-06-19 22:59:46.668029-0500 videommm[8918:255076] INFO:   libswresample   4.  0.100 /  4.  0.100
2022-06-19 22:59:46.668221-0500 videommm[8918:255076] WARNING: Trailing option(s) found in the command: may be ignored.
2022-06-19 22:59:46.701533-0500 videommm[8918:255076] INFO: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/Users/homefolder/Library/Developer/CoreSimulator/Devices/2FE26EEA-A16A-4DAA-A38E-D003C678A3A2/data/Containers/Bundle/Application/F4A3F78B-5582-41A0-8338-ADD05240F64D/videommm.app/Earth_Zoom_In.mov':
Command failed with state COMPLETED and rc 1.Unknown
2022-06-19 22:59:46.701781-0500 videommm[8918:255076] INFO:   Metadata:
2022-06-19 22:59:46.701973-0500 videommm[8918:255076] INFO:     major_brand     :
2022-06-19 22:59:46.702152-0500 videommm[8918:255076] INFO: qt
2022-06-19 22:59:46.702371-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.702541-0500 videommm[8918:255076] INFO:     minor_version   :
2022-06-19 22:59:46.702747-0500 videommm[8918:255076] INFO: 537199360
2022-06-19 22:59:46.702938-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.703085-0500 videommm[8918:255076] INFO:     compatible_brands:
2022-06-19 22:59:46.703304-0500 videommm[8918:255076] INFO: qt
2022-06-19 22:59:46.703584-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.703858-0500 videommm[8918:255076] INFO:     creation_time   :
2022-06-19 22:59:46.704101-0500 videommm[8918:255076] INFO: 2013-07-09T19:19:28.000000Z
2022-06-19 22:59:46.704327-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.704620-0500 videommm[8918:255076] INFO:   Duration:
2022-06-19 22:59:46.704846-0500 videommm[8918:255076] INFO: 00:00:06.84
2022-06-19 22:59:46.705150-0500 videommm[8918:255076] INFO: , start:
2022-06-19 22:59:46.705409-0500 videommm[8918:255076] INFO: 0.000000
2022-06-19 22:59:46.705632-0500 videommm[8918:255076] INFO: , bitrate:
2022-06-19 22:59:46.705909-0500 videommm[8918:255076] INFO: 9824 kb/s
2022-06-19 22:59:46.706185-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.706519-0500 videommm[8918:255076] INFO:   Stream #0:0
2022-06-19 22:59:46.706760-0500 videommm[8918:255076] INFO: [0x1]
2022-06-19 22:59:46.707083-0500 videommm[8918:255076] INFO: (eng)
2022-06-19 22:59:46.707377-0500 videommm[8918:255076] INFO: : Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080, 9812 kb/s
2022-06-19 22:59:46.707642-0500 videommm[8918:255076] INFO: , SAR 1:1 DAR 16:9
2022-06-19 22:59:46.707947-0500 videommm[8918:255076] INFO: ,
2022-06-19 22:59:46.708306-0500 videommm[8918:255076] INFO: 25 fps,
2022-06-19 22:59:46.708617-0500 videommm[8918:255076] INFO: 25 tbr,
2022-06-19 22:59:46.708969-0500 videommm[8918:255076] INFO: 25 tbn
2022-06-19 22:59:46.709169-0500 videommm[8918:255076] INFO:  (default)
2022-06-19 22:59:46.709332-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.709603-0500 videommm[8918:255076] INFO:     Metadata:
2022-06-19 22:59:46.709808-0500 videommm[8918:255076] INFO:       creation_time   :
2022-06-19 22:59:46.710117-0500 videommm[8918:255076] INFO: 2013-07-09T19:19:28.000000Z
2022-06-19 22:59:46.710660-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.710924-0500 videommm[8918:255076] INFO:       handler_name    :
2022-06-19 22:59:46.711139-0500 videommm[8918:255076] INFO: Apple Video Media Handler
2022-06-19 22:59:46.711488-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.711733-0500 videommm[8918:255076] INFO:       vendor_id       :
2022-06-19 22:59:46.712038-0500 videommm[8918:255076] INFO: appl
2022-06-19 22:59:46.712365-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.712698-0500 videommm[8918:255076] INFO:       encoder         :
2022-06-19 22:59:46.712971-0500 videommm[8918:255076] INFO: H.264
2022-06-19 22:59:46.713283-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.713563-0500 videommm[8918:255076] INFO:       timecode        :
2022-06-19 22:59:46.713863-0500 videommm[8918:255076] INFO: 00:00:00:00
2022-06-19 22:59:46.714037-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.714353-0500 videommm[8918:255076] INFO:   Stream #0:1
2022-06-19 22:59:46.714769-0500 videommm[8918:255076] INFO: [0x2]
2022-06-19 22:59:46.715138-0500 videommm[8918:255076] INFO: (eng)
2022-06-19 22:59:46.715510-0500 videommm[8918:255076] INFO: : Data: none (tmcd / 0x64636D74), 0 kb/s
2022-06-19 22:59:46.715889-0500 videommm[8918:255076] INFO:  (default)
2022-06-19 22:59:46.716143-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.716460-0500 videommm[8918:255076] INFO:     Metadata:
2022-06-19 22:59:46.716794-0500 videommm[8918:255076] INFO:       creation_time   :
2022-06-19 22:59:46.717141-0500 videommm[8918:255076] INFO: 2013-07-09T19:23:14.000000Z
2022-06-19 22:59:46.717427-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.717686-0500 videommm[8918:255076] INFO:       handler_name    :
2022-06-19 22:59:46.718053-0500 videommm[8918:255076] INFO: Tidskodsmediehanterare
2022-06-19 22:59:46.718412-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.718787-0500 videommm[8918:255076] INFO:       timecode        :
2022-06-19 22:59:46.719136-0500 videommm[8918:255076] INFO: 00:00:00:00
2022-06-19 22:59:46.719402-0500 videommm[8918:255076] INFO:
2022-06-19 22:59:46.719596-0500 videommm[8918:255076] FATAL: At least one output file must be specified
Xavi Font
  • 296
  • 4
  • 19
  • @WiseEye -- Then why would you comment at all? – Tim Roberts Jun 20 '22 at 03:35
  • The `bundle.main.url` function searches recursively. `ffmpeg` has to be given the exact path name. Does the file exist in a subfolder? – Tim Roberts Jun 20 '22 at 03:38
  • @TimRoberts Hey, so I added a screenshot of my file structure. Also, when I try to just simply play the video in the UI it does play, so at least that video playing library detects the video, but not Ffmpeg I guess... – Xavi Font Jun 20 '22 at 03:44
  • Does the file actually have a `.mov` extension? If you tell ffmpeg to place `Earth_Zoom_In` with no extension, does that work? – Tim Roberts Jun 20 '22 at 03:46
  • @TimRoberts yes, the file does have a .mov extension. I did however try out your recommendation of running `-i Earth_Zoom_In -c:v file1.mp4` but the same exact error appeared ` ERROR: Earth_Zoom_In: No such file or directory` – Xavi Font Jun 20 '22 at 03:50
  • `2022-06-19 22:59:46.719596-0500 videommm[8918:255076] FATAL: At least one output file must be specified` this is output from ffmpeg - you will need to fix your command - nothing to do with Swift or ffmpeg-kit – MadProgrammer Jun 20 '22 at 04:14

1 Answers1

2

Sooo, welcome to the wonderful world of "file handling in Swift/iOS"

First, you should probably take a look at:

... and, you know what, you could probably google for more

First Bundle.main.url is going to give a URL, which is helpful, but you need a String. It would start by using URL#path and test to see if that works, otherwise URL#description will be what you want (sorry, I'm vague, been awhile since I needed to this)

Next, you need somewhere to output the file to. iOS (and even MacOS) are sandboxed, so you must "ask" the system where you can write things to do. In this, I might consider using the "cache" directory, a few reasons, but mostly it won't expose the file to the user (you could store it in the "documents" directory if you wanted to)

So, all that might look something like...

guard let input = Bundle.main.url(forResource: "Earth_Zoom_In", withExtension: "mov") else { return }
guard let outputPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else { return }
let output = outputPath.appendingPathComponent("file1.mp4")

Then you can use them in your command something like...

FFmpegKit.execute("-i \(input.path) -c:v \(outputPath.path)")

Now, if this fails, you need to use FFmpegKit.execute("-i \(input) -c:v \(outputPath)") instead, which will produce a file:// based URL string

nb: Based on some, very old code, I was using URL#path

Notes

  • File management on iOS is much more restrictive then on MacOS, you are only allowed to write within the sandboxed constraints of the App.
  • If you wish to "share" the file, under iOS, the .documents directory is probably preferred. You could write directly to it, or do a move/copy operation later.
  • Under MacOS, you may not be allowed to write to certain locations without first having specified your intent in the app's "capability" and gotten permission from the user before hand
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Woah, my hero has returned lol... I tried what you told me (don't worry I will read the docs u sent too, I am not that useless lol) a new error appeared though `At least one output file must be specified` I posted the ENTIRE log for reference above, as you know, it is not much, just long. – Xavi Font Jun 20 '22 at 04:03
  • That error is from ffmpeg itself (I don't know, I copied the command from ffmpeg-kit examples ), I changed it to `FFmpegKit.execute("-i \"\(inputResource.path)\" -y \"\(outputResource.path)\"")` in my testing – MadProgrammer Jun 20 '22 at 04:08
  • @XaviFont I did a quick SwiftUI test (because I can't resist) - [TestFFmpegKitSwiftUI](https://github.com/RustyKnight/TestFFmpegKitSwiftUI). This was intended to work on MacOS (as it has a "show in finder" option), but the workflow should otherwise work on other platforms ... except watchOS, because that would be crazy – MadProgrammer Jun 20 '22 at 04:12
  • Woooooo!!!! Worked! So yes, this `FFmpegKit.execute("-i \"\(inputResource.path)\" -y \"\(outputResource.path)\"")` solved it, however I added `"-i \"\(input.path)\" -y \"\(outputPath.path)\"/new_video.mp4"` at the end since it was telling me it could not find a way to output and it looked like that ended in a directory. Thanks again so much! – Xavi Font Jun 20 '22 at 04:14
  • That's what `let output = outputPath.appendingPathComponent("file1.mp4")` was doing, it was "appending" the target file to the end of the target path ;) – MadProgrammer Jun 20 '22 at 04:16
  • wow, I googled a million times Swift UI Swift Ffmpeg iOS and nothi g came up, that link of the repo of TestFFmpegKitSwiftUI will be handy. Also, is it that rare for people to use Ffmpeg on swift? or are people just quiet about it lol. – Xavi Font Jun 20 '22 at 04:18
  • 1
    true, `let output = outputPath.appendingPathComponent("file1.mp4")` was doing that ‍️ `"-i \"\(input.path)\" -y \"\(output)\""` dos the trick – Xavi Font Jun 20 '22 at 04:20
  • It would suggest it might be a limited number of people who "need" to work with ffmpeg at this level, and since it's not really something you want to do be doing on iOS, it limits it's over usage. – MadProgrammer Jun 20 '22 at 04:49
  • @MadProgrammer how can I get the conversion progress? – Rashesh Bosamiya Feb 06 '23 at 12:04
  • @RasheshBosamiya From (poor) memory, I think you need to look at the one of the callbacks (maybe the log callback) and parse the text been returned, it's not as simple as a having a "progress" callback - see https://stackoverflow.com/questions/72672889/ffmpeg-for-use-in-ios-application-coded-in-swift/72673294?noredirect=1#comment132970645_72673294 for a starting point – MadProgrammer Feb 06 '23 at 20:36