I am concatenating images into a video using mobileffmpeg cocoapod using the command below but the video quality drops a bit when it has finished concatenating.
-f concat -i \(outputTxt) -b:v 8M -pattern_type sequence -r 25 \(output)
I can see the image array isn't the issue as the quality is good so how can I avoid losing quality when concatenating? I thought maybe using libx264 would solve the issue but it isn't supported by mobileffmpeg
Below are the functions I use in Swift to concatenate if it helps understand the process.
Thanks for your help!
func concatonateScreenshotsToVideo( completed: #escaping (URL) -> ()){
if compilingVideo {
self.delegate?.screenRecordingConcatonating()
var imgs = self.storedTempScreenshotImgArray()
imgs.sort()
self.createFFmpegTextFile(tempScreenshotImgArray: imgs) {
let filename = "screenshot_ffmpegData.txt"
let outputTxt = URL(fileURLWithPath: self.mainPath + "/Temp/").appendingPathComponent(filename)
let output = URL(fileURLWithPath: self.mainPath + "/Temp/").appendingPathComponent("screenshot.mp4")
let ffmpeg = "-f concat -i \(outputTxt) -b:v 8M -pattern_type sequence -r \(self.fps) \(output)"
MobileFFmpeg.execute(ffmpeg)
completed(output)
}
} else {
terminateFFmpeg()
return
}
}
func createFFmpegTextFile(tempScreenshotImgArray: [String], completed: () -> Void){
let filename = "screenshot_ffmpegData.txt"
let textFile = URL(fileURLWithPath: mainPath).appendingPathComponent("Temp/\(filename)")
for img in tempScreenshotImgArray {
autoreleasepool {
do {
let fileHandle = try FileHandle(forWritingTo: textFile)
fileHandle.seekToEndOfFile()
let filepath = "file \(img)\n duration 0.04\n"
fileHandle.write(filepath.data(using: .utf8)!)
} catch {
do {
let filePath = "file \(img)\n duration 0.04\n"
try filePath.write(to: textFile, atomically: false, encoding: .utf8)
} catch {
DebugPrint.DBprint("FFmpeg manager error writing text file: \(error.localizedDescription)")
}
}
}
}
func storedTempScreenshotImgArray() -> [String] {
var screenshotImgArray: [String] = []
guard let dataFilePath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("/Temp") else {return screenshotImgArray}
do {
let fileUrls = try fileManager.contentsOfDirectory(at: dataFilePath, includingPropertiesForKeys: nil)
for jpgFile in fileUrls {
if jpgFile.pathExtension == "jpg" && !screenshotImgArray.contains(jpgFile.lastPathComponent) {
screenshotImgArray.append(jpgFile.lastPathComponent)
}
}
} catch {
DebugPrint.DBprint("screenshot - Error while enumerating folder segment")
}
return screenshotImgArray
}
use :
pod 'mobile-ffmpeg-full-gpl'
that support's x264 and x265
in here you can read about mobile ffmpeg versions
for using in swift you should add ProjectName-Bridging-Header.h to your project and then import the library in that: #import <mobileffmpeg/MobileFFmpeg.h>
Related
I am trying to delete any empty folders in a directory (sometimes folders within folders).
Here's how I'm trying to do it (see below), however it still leaves the last folder.
If there is a better way to approach this?
func removeEmptyFoldersAt(url: URL) {
let folderContents = try? fm.contentsOfDirectory(at: url, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsPackageDescendants, .skipsHiddenFiles]).filter({u in
let attr = try? u.resourceValues(forKeys: [.isDirectoryKey])
return attr!.isDirectory!
})
for folder in folderContents! {
let attr = try? folder.resourceValues(forKeys: [.isDirectoryKey])
let contents = try? fm.contentsOfDirectory(at: folder, includingPropertiesForKeys: [], options: [.skipsHiddenFiles,.skipsPackageDescendants])
if attr!.isDirectory! && contents!.count > 0 {
removeEmptyFoldersAt(url: folder)
}
if attr!.isDirectory! && contents?.count == 0 {
try? fm.removeItem(at: folder)
}
}
}
I'd follow a slightly different workflow, I'd have the removeEmptyFoldersAt be more generic in its workflow.
So you can pass it URL and if:
it's a directory, list the contents and recursively call itself with each item
it's a file, just remove it
For example...
extension URL {
var isDirectory: Bool {
return (try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory ?? false
}
}
func removeItem(at url: URL) throws {
let fm = FileManager.default
if url.isDirectory {
for item in try fm.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: []) {
try removeItem(at: item)
}
}
try fm.removeItem(at: url)
}
nb: I've not tested this, but this is a common workflow I've used in the past
I have a python socket server that sends a PNG file (72755 bytes) whenever receives "send" from the client. The client python code receives 72755 bytes and saves the file correctly:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send('send')
f = open('torecv.png','wb')
l = s.recv(1024)
while (l):
f.write(l)
l = s.recv(1024)
k = str(l)
byte_num += len(l)
if k[-5:-1] == 'done':
break
f.write(l[:-4])
f.close()
s.close()
However, in Swift, the following code gets a total of 72755 bytes and gives me a corrupted PNG file. Can anyone tell me where is the problem? Thank you
switch client.connect(timeout: 1) {
case .success:
switch client.send(string: "send" ) {
case .success:
var dataAll : [UInt8] = []
while true {
guard let data = client.read(1024, timeout:1) else { break }
print("bytes: " + String(data.count))
if String(bytes: data, encoding: .utf8) == "done" {
print("done")
}else{
dataAll += data
}
}
print(dataAll.count) // prints 72755
let img : Data = Data(bytes: dataAll, count: dataAll.count)
writeToFile(data: img, fileName: "testPng")
Edit:
The problem could be in saving PNG file (Both Python and Swift get all bytes correctly)
func writeToFile(data: Data, fileName: String){
// get path of directory
guard let directory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else {
return
}
// create file url
let fileurl = directory.appendingPathComponent("\(fileName).png")
print(fileurl)
// if file exists then write data
if FileManager.default.fileExists(atPath: fileurl.path) {
if let fileHandle = FileHandle(forWritingAtPath: fileurl.path) {
// seekToEndOfFile, writes data at the last of file(appends not override)
fileHandle.seekToEndOfFile()
fileHandle.write(data)
fileHandle.closeFile()
var yourImage: UIImage = UIImage(named: fileurl.path)!
testImg.image = yourImage
}
else {
print("Can't open file to write.")
}
}
else {
// if file does not exist write data for the first time
do{
try data.write(to: fileurl, options: .atomic)
}catch {
print("Unable to write in new file.")
}
}
}
Main Image and what Python gets:
What Swift gets:
I have the following code which writes to the end of a file. However, how do I modify the behaviour so that it creates "output.txt" if it doesn't exist?
Thanks in advance!
func writeFile(){
let str = "\n" + nominationKeyForWhenCellSelected! + "," + "1"
let datafromString = str.data(using: String.Encoding.utf8)
let filename = getDocumentsDirectory().appendingPathComponent("output.txt")
do {
let fileHandle = try FileHandle(forWritingTo: filename)
fileHandle.seekToEndOfFile()
fileHandle.write(datafromString!)
} catch {
}
}
Use FileManager to check if the file exists or not. If not, simply write the data, otherwise append as you are doing. But don't forget to close the file handle.
do {
if FileManager.default.fileExists(atPath: filename.path) {
let fileHandle = try FileHandle(forWritingTo: filename)
fileHandle.seekToEndOfFile()
fileHandle.write(datafromString!)
fileHandle.closeFile()
} else {
datafromString.write(to: filename)
}
} catch {
print(error)
}
I'm using the swift ubuntu docker: https://github.com/IBM-Swift/swift-ubuntu-docker
and try to copy a file from pathA to pathB. During the execution I get the fatal error:
fatal error: copyItem(atPath:toPath:) is not yet implemented: file Foundation/NSFileManager.swift, line 376
Illegal instruction
The command:
# swift --version
responses
Swift version 3.1.1 (swift-3.1.1-RELEASE)
Target: x86_64-unknown-linux-gnu
Online I found the information that it should be implemented:
https://bugs.swift.org/browse/SR-2639
Can someone help out? Thanks!
copyItem(atPath:toPath:) is not implemented on the Swift 3.1 branch
of the Foundation framework for Linux:
open func copyItem(atPath srcPath: String, toPath dstPath: String) throws {
NSUnimplemented()
}
What you can for example do is
let fm = FileManager.default
if let contents = fm.contents(atPath: srcPath) {
if !fm.createFile(atPath: destPath, contents: contents, attributes: nil) {
print("cannot write destination file")
}
} else {
print("cannot read source file")
}
which is a simplified version of how copyItem(atPath:toPath:)
is implemented on the master branch.
If the file is very large then you may want to copy in chunks
instead of reading the entire file into memory, for example like this:
guard let srcFile = FileHandle(forReadingAtPath: srcPath) else {
fatalError("cannot open source file")
}
guard let destFile = FileHandle(forWritingAtPath: destPath) else {
fatalError("cannot open destination file")
}
while case let data = srcFile.readData(ofLength: 1024 * 1024), data.count > 0 {
destFile.write(data)
}
srcFile.closeFile()
destFile.closeFile()
Thanks that works fine. I found in the meanwhile another solution which also works ;-)
let data = try Data.init(contentsOf: URL.init(fileURLWithPath: path))
guard FileManager.default.createFile(atPath: url.path, contents: data, attributes: nil) else {
print("Can not read/create the file")
return false
}
I am using my phone to record some sensor data and store it on device via SQLite via SharkORM(DBAccess).
I now want to write that data out to a CSV file however, I am now up to 1.6 million records.
Currently, I am looping through 1000 records, adding them to a string and at the end writing them out. However, there must be a better way to do it?
func writeRawFile()
{
let fileName = "raw"
let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileURL = DocumentDirURL.appendingPathComponent(fileName).appendingPathExtension("csv")
var data = "time,lat,lon,speed,x_acc,y_acc,z_acc,gyro_x,gyro_y,gyro_z,compass,orientation\r\n"
let count = RawReading.query().count()
var counter = 0;
let df = DateFormatter()
df.dateFormat = "y-MM-dd H:m:ss.SSSS"
for i in stride(from:0, to: count, by: 1000)
{
autoreleasepool {
for result in RawReading.query().offset(Int32(i)).limit(1000).fetch()
{
if let raw : RawReading = result as? RawReading
{
if (Double(raw.speed!) > 3.0) //1 Meter per Second = 2.236936 Miles per Hour
{
//print(df.string(from: raw.date!))
data += "\(df.string(from: raw.date!)),\(raw.lat!),\(raw.lon!),\(raw.speed!),\(raw.x!),\(raw.y!),\(raw.z!),\(raw.xx!),\(raw.yy!),\(raw.zz!),\(raw.compass!),\(raw.orientation!)" + "\r\n"
counter += 1
}
}
}
print ("Written \(i) of \(count)")
}
}
print("Count \(count) \(counter)")
//write the file, return true if it works, false otherwise.
do{
try data.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8 )
} catch{
print("error")
}
}
Open a FileHandle for writing, then build and write each line separately, so that you don't have to keep the entire file contents
in memory:
do {
let file = try FileHandle(forWritingTo: fileURL)
defer { file.closeFile() }
for <... your loop ...> {
let line = ... // build one CSV line
file.write(line.data(using: .utf8)!)
}
} catch let error {
// ...
}
You can also write to a temporary file first and then rename it to the
actual file, in order to avoid a damaged file if anything
went wrong.